* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
*
* PROJECT:			Do financial concerns make workers less productive?
* RESEARCHERS:		Supreet Kaur, Sendhil Mullainathan, Suanna Oh, Frank Schilbach
* TASK:				Generate variables for analysis
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 


/* GENERAL
- data at the plate level: unique id = pid day slot Serial_number
- there is no round 4 in the data -- this round was cancelled; 
	so round 5 in the data correspodents to round 4 described in the paper, and similarly for the rest
- note that the variable 'day' is not always consecutive (i.e., might skip over), as it corresponds to when events happen
 
* NOTES ON DATA STRUCTURE

* ROUNDS 5 - 15
- Hour_Start tells us the calendar hour in which the slot takes place
- on non-priming days, always 5 hour-long slots (Hour_Start)
- on priming days, priming always happened btwn 9:30-10 for all
- if you got primed, we observe one 30 min slot for you (slot 1) & slot 2 is missing
- if you didnt get primed, then we observe two 30 min slots for you (slot 1 and slot 2)
- so on non-priming days, slot3 is 11 am Hour_Start, and on priming days, slot3 is 10 am Hour_Start
- Note that on priming days, if anyone was primed, everyone had 2 halfhour slots from 9-10,
  including the wave for whom it was not their priming day.
- So, in analysis, if we want time of day (ie. calendar hour) FE, 
  put in FE for Hour_Start, or equivalently, for time, and then also
  dont put in dummy for "halfslot" FE to account for some half_hour slots - correlated with treatment.
 
* ROUNDS 1 - 3
- the workday was longer: 7 or 8 hourly or semi-hourly slots
- during priming slots, non-priming participants did not work and engaged in control versions of the sessions
- Some end-of-day survey and breakfast consumption variables were added after Round 3
*/


* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
*					<< Sections >>
* 
*		0.  AGGREGATE VARIABLES TO THE SLOT LEVEL
*		1.  BASIC VARS
*		2.  CASH AND PRIMING COVARIATES
*		3.  LEADS AND LAGS
*		4.  PRODUCTIVITY VARIABLES
*		5.  ATTENTION MEASURES
*		6.  BASELINE SURVEY
*		7.  EXPENDITURE
*		8.  NUTRITION
*		9.  EXIT SURVEY
*		10. OTHER VARIABLES
*		11. BASELINE PROD AND ATTENTION VARS
*		12. WEALTH HETEROGENEITY + SAVE ALL ROUNDS
*		13. LASSO
*		14. SAVE MAIN ROUNDS DATA
* 		
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


***************************************************************** 
*		0. AGGREGATE VARIABLES TO THE SLOT LEVEL
*****************************************************************


use "$dir/data/financial_strain.dta", clear


* Aggregate attention measures at the slot level 
	gen numleaf = number_leaves			// shorten name
	* raw vars		// NOTE: R1-3 did not collect num_stitches
	foreach v of varlist numleaf num_double_holes num_stitches {
		gen `v'r = `v'
			replace `v'r = . if `v'==-555
			egen avg_`v'r = mean(`v'r), by(round pid day slot) 
	}

* gen tags
sort round pid day slot Serial_number 	
by round pid day slot: gen pidslot1 = 1 if _n==1
by round pid day: gen pidday1 = 1 if _n==1
by round pid: gen pid1 = 1 if _n==1

keep if pidslot1 == 1
drop Serial_number


***************************************************************** 
*		1. BASIC VARS
*****************************************************************

egen roundgroup = group(round group)

gen survsample = (pid!=97) & round<=15						// not missing baseline

* date vars
gen year = year(date)

** WAVES  (for R1-15)
egen temp = group(wave)
gen wavenum = 1 if temp==1					
	replace wavenum = 2 if temp== 3
	replace wavenum = 3 if temp== 5
	replace wavenum = 4 if temp== 7
	replace wavenum = 5 if temp== 9
	replace wavenum = 6 if temp== 11
	replace wavenum = 7 if temp== 2
	replace wavenum = 8 if temp== 4
	replace wavenum = 9 if temp== 6
	replace wavenum = 10 if temp== 8
	replace wavenum = 11 if temp== 10
	replace wavenum = 12 if temp== 12
	drop temp
	* recode for rounds 1-3: those skipped waves 4 and 10
	replace wavenum = 5 if wave=="W4" & round<=3
	replace wavenum = 6 if wave=="W5" & round<=3
	replace wavenum = 11 if wave=="W4b" & round<=3
	replace wavenum = 12 if wave=="W5b" & round<=3
	
gen wavea = group==1 if round<=15
gen waveb = group==2 if round<=15

* Attendance
gen attend = (Attendance==0)		// not consistent at day level on purpose
replace attend = . if (Attendance==3 | Attendance==4 | Attendance==5)

gen present_day = (Attendance==0) | (Attendance==5)			// in some rounds workers were present but first slot of second day was not collected
replace present_day = 1 if round == 17 & pid == 163 & day == 4 & slot == 5		// left worksite early
replace present_day = . if (Attendance==3 | Attendance==4)


***************************************************************** 
*		2. CASH AND PRIMING COVARIATES
*****************************************************************


** cash and gotcash
gen cash = inlist(wavenum,1,2,3,7,8,9)									// cash groups

// postcash: days after cash payments were given out by wave
gen postcash = 0			
replace postcash = 1 if day>=9  & wavea==1 & round==1
replace postcash = 1 if day>=10 & waveb==1 & round==1
replace postcash = 1 if day>=10 & wavea==1 & inrange(round,2,3)
replace postcash = 1 if day>=11 & waveb==1 & inrange(round,2,3)
replace postcash = 1 if day>=9  & wavea==1 & inrange(round,5,13)
replace postcash = 1 if day>=10 & waveb==1 & inrange(round,5,13)
replace postcash = 1 if day>=4  & wavea==1 & inrange(round,14,15)
replace postcash = 1 if day>=5  & waveb==1 & inrange(round,14,15)

gen gotcash = postcash*cash


* day that postcash=1 for people (defined for both control and treatment based on postcash)
egen temp = min(day) if postcash==1, by(round pid)
egen wavecashday = max(temp), by(round pid)
drop temp
	* separately for wave a 
	egen temp = min(day) if postcash==1 & wavea==1, by(round)
	egen wavecashday_wa = max(temp), by(round)
	drop temp 


* experience proxies - length of round
gen exper = wavecashday - firstday
gen expersq = exper^2


** Salience groups
gen fincashpoor = inlist(wavenum,1,4,5,7,10,11)							// cashpoor salience
gen fincashrich = inlist(wavenum,2,8)									// cashrich salience
	
// poststory: first slot after story for R1-3

gen poststory1 = 2 if primingorder1==1 
replace poststory1 = 3 if primingorder1==2 
replace poststory1 = 3 if primingorder1==3 

gen poststory2 = 2 if primingorder2==1 
replace poststory2 = 3 if primingorder2==2 
replace poststory2 = 3 if primingorder2==3 


// postprime
gen postprime1 = 0 				// post priming hours on priming days 1
replace postprime1 = 1 if day==dayofpriming1 & slot>=poststory1 & inrange(round,1,3)	// R1-3
replace postprime1 = 1 if day==6 & slot>=3 & wavea==1 & inrange(round,5,13)   			// R5-13: on day 6 or 7
replace postprime1 = 1 if day==7 & slot>=3 & waveb==1 & inrange(round,5,13)
replace postprime1 = 1 if day==3 & slot>=3 & wavea==1 & inrange(round,14,15)			// R14-15: on day 3 or 4
replace postprime1 = 1 if day==4 & slot>=3 & waveb==1 & inrange(round,14,15)

gen postprime2 = 0 				// post priming hours on priming days 2
replace postprime2 = 1 if day==dayofpriming2 & slot>=poststory2 & inrange(round,1,3)	// R1-3
replace postprime2 = 1 if day==10 & slot>=3 & wavea==1 & inrange(round,5,13)   			// R5-13: on day 10 or 11
replace postprime2 = 1 if day==11 & slot>=3 & waveb==1 & inrange(round,5,13)
replace postprime2 = 1 if day==5 & slot>=3 & wavea==1 & inrange(round,14,15)			// R14-15: on day 5 or 6
replace postprime2 = 1 if day==6 & slot>=3 & waveb==1 & inrange(round,14,15)


// gotfin: post financial priming 
gen gotfin = 0
replace gotfin = 1 if inlist(wavenum,1,4,7,10) & postprime1==1 
replace gotfin = 1 if inlist(wavenum,2,5,8,11) & postprime2==1

gen gotcashpoorfin = fincashpoor*gotfin
gen gotcashrichfin = fincashrich*gotfin

drop poststory1 poststory2 postprime1 postprime2


** Post-sal var - post among eligibles who could have been primed that day
	* postsal before and after payday
	gen postsal1 = 0
		**** rounds 5-15
		replace postsal1 = 1 if round>=5 & round<=13  & slot>=3 & (day==6 | day==7)
		replace postsal1 = 1 if round>=14 & round<=15 & slot>=3 & (day==3 | day==4) 
		**** rounds 1 - note this wont be perfect bc slot times are diff for primed and non-primed on wsfinday's
		replace postsal1 = 1 if round==1 & (day==7 & wavea==1) & primingorder1==1 & slot>=2						// wavea does priming on day 7
		replace postsal1 = 1 if round==1 & (day==7 & wavea==1) & primingorder1>=2 & slot>=3			
		replace postsal1 = 1 if round==1 & (day==7 & waveb==1) & slot>=3 										// waveb has non-priming schedule on day 7
		
		replace postsal1 = 1 if round==1 & (day==8 & waveb==1) & primingorder1==1 & slot>=2						// waveb does priming on day 8		
		replace postsal1 = 1 if round==1 & (day==8 & waveb==1) & primingorder1>=2 & slot>=3		
		replace postsal1 = 1 if round==1 & (day==8 & wavea==1) & slot>=3										// wavea has non-priming schedule on day 8
		**** rounds 2-3
		replace postsal1 = 1 if (round==2 | round==3) & (day==8 & wavea==1) & primingorder1==1 & slot>=2		// wavea does priming on day 8
		replace postsal1 = 1 if (round==2 | round==3) & (day==8 & wavea==1) & primingorder1>=2 & slot>=3			
		replace postsal1 = 1 if (round==2 | round==3) & (day==8 & waveb==1) & slot>=3 							// waveb has non-priming schedule on day 8
	
		replace postsal1 = 1 if (round==2 | round==3) & (day==9 & waveb==1) & primingorder1==1 & slot>=2		// waveb does priming on day 9
		replace postsal1 = 1 if (round==2 | round==3) & (day==9 & waveb==1) & primingorder1>=2 & slot>=3		// waveb does priming on day 9
		replace postsal1 = 1 if (round==2 | round==3) & (day==9 & wavea==1) & slot>=3							// wavea has non-priming schedule on day 9

	gen postsal2 = 0
		**** rounds 5-15
		replace postsal2 = 1 if round>=5 & round<=13  & slot>=3 & (day==10 | day==11)
		replace postsal2 = 1 if round>=14 & round<=15 & slot>=3 & (day==5 | day==6) 
		**** rounds 1 
		replace postsal2 = 1 if round==1 & (day==10 & wavea==1) & primingorder2==1 & slot>=2					// wavea does priming on day 10
		replace postsal2 = 1 if round==1 & (day==10 & wavea==1) & primingorder2>=2 & slot>=3		
		replace postsal2 = 1 if round==1 & (day==10 & waveb==1) & slot>=3 										// waveb has non-priming schedule on day 10

		replace postsal2 = 1 if round==1 & (day==11 & waveb==1) & primingorder2==1 & slot>=2					// waveb does priming on day 11
		replace postsal2 = 1 if round==1 & (day==11 & waveb==1) & primingorder2>=2 & slot>=3	
		replace postsal2 = 1 if round==1 & (day==11 & wavea==1) & slot>=3										// wavea has non-priming schedule on day 11
		**** rounds 2-3
		replace postsal2 = 1 if (round==2 | round==3) & (day==11 & wavea==1) & primingorder2==1 & slot>=2		// wavea does priming on day 11
		replace postsal2 = 1 if (round==2 | round==3) & (day==11 & wavea==1) & primingorder2>=2 & slot>=3		
		replace postsal2 = 1 if (round==2 | round==3) & (day==11 & waveb==1) & slot>=3 							// waveb has non-priming schedule on day 11
	
		replace postsal2 = 1 if (round==2 | round==3) & (day==12 & waveb==1) & primingorder2==1 & slot>=2		// waveb does priming on day 12
		replace postsal2 = 1 if (round==2 | round==3) & (day==12 & waveb==1) & primingorder2>=2 & slot>=3		// waveb does priming on day 12
		replace postsal2 = 1 if (round==2 | round==3) & (day==12 & wavea==1) & slot>=3							// wavea has non-priming schedule on day 12

	gen postsal = postsal1 + postsal2
	gen postcashpoorsal = postsal
	replace postcashpoorsal = 0 if postsal2==1 & cash==1 	

// above undefined for R16-20
foreach x of varlist cash postcash gotcash wavecashday wavecashday_wa  exper expersq fincashpoor fincashrich gotfin gotcashpoorfin gotcashrichfin postsal1 postsal2 postsal postcashpoorsal{
	replace `x'=. if round>15
}	
	
label var gotcash "Cash $\times$ Post-pay" 
label var postcash "Post-pay"
label var gotfin "Prime group $\times$ Post-priming"
label var gotcashpoorfin "Prime group $\times$ Post-priming $\times$ Pre-payment"
label var postsal "Post-priming intervention"
label var postcashpoorsal "Post-priming $\times$ Pre-payment"
	
	
***** Got priming

* got late prime
gen temp1 = gotfin==1 & postsal2==1 

egen lateprime = max(temp1), by(pid)
drop temp1

** hour after priming
gen temp1 = slot if postsal==1
sort round pid day temp1
by round pid day: gen temp2 = _n if postsal==1
rename temp2 postsal_hrn
drop temp1

gen postsal_hr1 = postsal_hrn==1
gen postsal_hr2 = postsal_hrn==2
gen postsal_hr1_2 = (postsal_hr1==1 | postsal_hr2==1)
gen postsal_hr2p = postsal==1 & postsal_hrn!=1
gen postsal_hr3p = postsal==1 & postsal_hrn!=1 & postsal_hrn!=2

gen gotfin_hr1 = postsal_hrn==1 & gotfin==1
gen gotfin_hr2 = postsal_hrn==2 & gotfin==1
gen gotfin_hr1_2 = (gotfin_hr1==1 | gotfin_hr2==1)
gen gotfin_hr2p = gotfin==1 & postsal_hrn!=1
gen gotfin_hr3p = gotfin==1 & postsal_hrn!=1 & postsal_hrn!=2

gen postcashpoorsal_hrn = postsal_hrn if postcashpoorsal==1

gen postcashpoorsal_hr1 = postcashpoorsal_hrn==1
gen postcashpoorsal_hr2 = postcashpoorsal_hrn==2
gen postcashpoorsal_hr1_2 = (postcashpoorsal_hr1==1 | postcashpoorsal_hr2==1)
gen postcashpoorsal_hr2p = postcashpoorsal==1 & postcashpoorsal_hrn!=1
gen postcashpoorsal_hr3p = postcashpoorsal==1 & postcashpoorsal_hrn!=1 & postcashpoorsal_hrn!=2

gen gotcashpoorfin_hr1 = postcashpoorsal_hrn==1 & gotcashpoorfin==1
gen gotcashpoorfin_hr2 = postcashpoorsal_hrn==2 & gotcashpoorfin==1
gen gotcashpoorfin_hr1_2 = (gotcashpoorfin_hr1==1 | gotcashpoorfin_hr2==1)
gen gotcashpoorfin_hr2p = gotcashpoorfin==1 & postcashpoorsal_hrn!=1
gen gotcashpoorfin_hr3p = gotcashpoorfin==1 & postcashpoorsal_hrn!=1 & postcashpoorsal_hrn!=2


foreach x of varlist gotfin* gotcashpoorfin* postsal* postcashpoorsal*{
	gen `x'_t1 = `x'==1 & postsal1==1
	gen `x'_t2 = `x'==1 & postsal2==1
}

	

***************************************************************** 
*		3. LEADS AND LAGS 
*****************************************************************


** Announcement cash structure
gen announceday = 5 if round <= 12 				// R1-12 - announcement in the morning of day 5	
	replace announceday = 6 if round==13   		// R13 - announcement in the morning of day 6. no day 5 in that round
* round 14 there was no announcement, it was surprise. So we define the announcement day as the day after treatment for first group treated.
	replace announceday = . if round==14
	replace announceday = 2 if round==15 	    // Workers knew since first workday what the schedule was
 
gen post_announce = (day>= announceday)
	replace post_announce = 1 if postcash==1
gen pre_announce  = 1 - post_announce

*** Generate announcement event time variables
cap drop event_time_announce
gen event_time_announce = .
replace event_time_announce = day - 4  if round<=12
replace event_time_announce = day - 5  if round==2 & day>=7
replace event_time_announce = day - 5  if round == 13
replace event_time_announce = event_time_announce + 1 if round == 13 & event_time_announce < 0
replace event_time_announce = .  if round == 14 
replace event_time_announce = day - 1  if round == 15

* Define event_time_announce in the day treatment cash is received (which is done by the end of the day) as -1
replace event_time_announce = event_time_announce - 1 if event_time_announce <= 0


*** Generate event time variables
cap drop event_time_pay
gen event_time_pay = .
replace event_time_pay = day - 8  if wavea==1 & round==1
replace event_time_pay = day - 9  if waveb==1 & round==1
replace event_time_pay = day - 9  if wavea==1 & round==2
replace event_time_pay = day - 10 if waveb==1 & round==2
*Correct for the fact that there is no day 6 in round 2
replace event_time_pay = event_time_pay + 1 if day<=5 & round==2

replace event_time_pay = day - 9  if wavea==1 & round==3
replace event_time_pay = day - 10 if waveb==1 & round==3
replace event_time_pay = day - 8  if wavea==1 & inrange(round,5,13) 
replace event_time_pay = day - 9  if waveb==1 & inrange(round,5,13)

*Correct for the fact that there is no day 5 in round 13
replace event_time_pay = event_time_pay + 1  if day<=5 & round == 13 
replace event_time_pay = day - 3  if wavea==1 & inrange(round,14,15) 
replace event_time_pay = day - 4  if waveb==1 & inrange(round,14,15)

* Define event_time of payment day (which is done by the end of the day) as -1
replace event_time_pay = event_time_pay - 1 if event_time_pay <= 0


* Define event_time_cash for control group as -999
gen event_time_cash = event_time_pay
replace event_time_cash = -999 if cash == 0


** generate post announcement/pre-cash group
cap drop post_announce_pre_cash 
gen papc = (post_announce == 1)*(postcash == 0)

gen papc_cash = papc*cash

*** Pay day dummy for cash groups
gen payday = (event_time_pay == -1)
	replace payday = 0 if round==14           // round 14 payment not anticipated on payday
gen payday_cash = payday*cash

gen papc2 = papc							  // papc that equals 0 on payday
replace papc2 = 0 if payday==1
gen papc_cash2 = papc_cash
replace papc_cash2 = 0 if payday_cash==1

** Code to differentiate early groups I and II
gen payday_cash_wa = payday_cash*wavea
gen payday_cash_wb = payday_cash*waveb

gen payday_wa = (payday*wavea)
gen payday_wb = (payday*waveb)


gen pre_announce_1  = (event_time_announce == -1)*(postcash==0)
gen pre_announce_2  = (event_time_announce == -2)*(postcash==0)
gen pre_announce_3  = (event_time_announce == -3)*(postcash==0)
gen post_announce_1  = (event_time_announce == 1)*(postcash==0)
gen post_announce_2  = (event_time_announce == 2)*(postcash==0)
gen post_announce_3p  = (event_time_announce >=3 & event_time_announce<.)*(postcash==0)

gen pre_announce_1_cash  = (event_time_announce == -1)*cash*(postcash==0)
gen pre_announce_2_cash  = (event_time_announce == -2)*cash*(postcash==0)
gen pre_announce_3_cash  = (event_time_announce == -3)*cash*(postcash==0)
gen post_announce_1_cash  = (event_time_announce == 1)*cash*(postcash==0)
gen post_announce_2_cash  = (event_time_announce == 2)*cash*(postcash==0)
gen post_announce_3p_cash  = (event_time_announce >=3 & event_time_announce<.)*cash*(postcash==0)


gen post_pay_1     = (event_time_pay == 1)
gen post_pay_2     = (event_time_pay == 2)
gen post_pay_3     = (event_time_pay == 3)
gen post_pay_4_plus     = (event_time_pay >= 4 & event_time_pay<.)
gen post_cash_1     = (event_time_cash == 1)
gen post_cash_2     = (event_time_cash == 2)
gen post_cash_3     = (event_time_cash == 3)
gen post_cash_4_plus     = (event_time_cash >= 4 & event_time_cash<.)		 
  
 
label var gotcash "Cash $\times$ Post-pay" 
label var payday_cash "Cash $\times$ Payment day" 
label var payday_cash_wb "Cash $\times$ Payment day $\times$ Wave B" 
label var papc "Post-announcement"
label var papc_cash "Cash $\times$ Announcement period"  
label var papc_cash2 "Cash $\times$ Announcement period"  
label var payday_wb "Wave B $\times$ Payment day"

label var pre_announce_2_cash "Announce cash $\times$ Pre 2 days"
label var pre_announce_1_cash "Announce cash $\times$ Pre 1 day"
label var post_announce_1_cash "Cash $\times$ 1 day post announcement"
label var post_announce_2_cash "Cash $\times$ 2 days post announcement"
label var post_announce_3p_cash "Announce cash $\times$ Post 3+ days"



***************************************************************** 
*		4. PRODUCTIVITY VARIABLES
*****************************************************************


* Production 
	gen output_tot = Total_Production if collect_prod==1 
	// collect_prod: worker is present (and not in training) AND not doing priming
	replace output_tot = 0 if Attendance==1 & primingtime==0 // tot coded as 0 if absent 
	replace output_tot = . if inlist(Attendance,3,4,5)

	
* Accepted production
	gen output = Acceptable_Production if output_tot!=. & attend!=.
		replace output = 0 if output_tot!=. & attend==0
	
	* daily accepted output
	egen temp = total(output) if pidslot1==1, by(round pid day)
	egen day_output = max(temp), by(round pid day)
	drop temp
	
* Rejected production
	gen output_rej = Rejected_Production if output_tot!=. & attend!=.
		replace output_rej = 0 if output_tot!=. & attend==0
	gen output_rej_sh = 1-(output/output_tot)
	gen output_rej_any = (output_rej>0) if output_rej!=.
	
	*** Half slots
	* Multiply prod in short slots to make them hour's worth
	gen productivity       = output
	 replace productivity  = output*2 if halfslot==1    /* 30 min slots */
	 replace productivity  = output*3 if lastslot20==1 /* 20 min slots */
	
	*** Normalize productivity
	summ productivity if postcash==1 & cash == 0 & round<=15 
	gen n_productivity = (productivity)/r(sd)
	
	*** Productivity including rejection
	gen productivity_tot       = output_tot
	 replace productivity_tot  = output_tot*2 if halfslot==1    /* 30 min slots */
	 replace productivity_tot  = output_tot*3 if lastslot20==1 /* 20 min slots */
	 
	*** Normalize productivity
	summ productivity_tot if postcash==1 & cash == 0 & round<=15	
	gen n_productivity_tot = (productivity_tot)/r(sd)


	* slots where surveys interfered with production
gen disrupt=(disruption_exp==1 | disruption_exit ==1 | disruption_qual==1)		// defined for R5-11, R13-15

label var n_productivity "Hourly production (normalized)"
label var productivity "Hourly production"
label var output_tot "Hourly production (incl. rejections)"
label var disrupt "Disrupted production on last day"
	
***************************************************************** 
*		5. ATTENTION MEASURES
*****************************************************************
	

* Normalize attention variables
	* normalize attention variables by mean and sd of the control group after payment
	* _use only data from r5-r15 for the baseline calculation since we don't measure all attention vars. in r1-r3
	foreach v of varlist avg_numleafr avg_num_double_holesr avg_num_stitchesr {
		egen temp1 = mean(`v') if postcash==1 & cash == 0 & round<=15	
		egen blm_`v' = max(temp1)
		egen temp2 = sd(`v') if postcash==1 & cash == 0 & round<=15
		egen blsd_`v' = max(temp2)
		drop temp*
		
		gen n_`v' = (`v' - blm_`v')/blsd_`v' 
	}
	
*** Attention indices
	** 3 measures: leaves, double holes, stitches
	egen att_ind     = rowmean(n_avg_numleafr n_avg_num_double_holesr n_avg_num_stitchesr)	
	replace att_ind  = (n_avg_numleafr + n_avg_num_double_holesr)/2  if n_avg_num_stitchesr==. & n_avg_numleafr!=.
	replace att_ind = - att_ind
	

		
*** ATTENTIVENESS PCA SCORE ***

	** PCA rescale for early rounds

	pca n_avg_numleafr n_avg_num_double_holesr n_avg_num_stitchesr if round <= 15, covar
	predict check
	mat W = e(L)
	local sum1 = W[1,1] + W[2,1] + W[3,1]
	local sum2 = W[1,1] + W[2,1]
	local w1  = W[1,1]*`sum1'/`sum2'
	local w2  = W[2,1]*`sum1'/`sum2'
	
	gen att_ind_pca = n_avg_numleafr*W[1,1] + n_avg_num_double_holesr*W[2,1] + n_avg_num_stitchesr*W[3,1] if round <= 15 & round>3 
		replace att_ind_pca = n_avg_numleafr*`w1' + n_avg_num_double_holesr*`w2' if round <= 3
		replace att_ind_pca = - att_ind_pca
		
	** Dummy variables above average for each index or component
	
	foreach var in att_ind att_ind_pca{
		cap drop hi`var'
		qui sum `var' if round<=15, det
		gen hi`var' = `var' > r(p50) if `var'!=.
	}
	

		
***************************************************************** 
*		6. BASELINE SURVEY
*****************************************************************


** DEMOGRAPHICS
	* age
	gen age_m = age if age>0
	replace age = . if age < 0
	gen age_nm = age_m
		replace age_nm = 0 if age_m==.
		gen age_miss = (age_m==.)

	* dummies for marital status
	gen married = (marital_status==1) if marital_status!=.
	gen widowed = (marital_status==3) if marital_status!=.
	gen divorced = (marital_status==4) if marital_status!=.

	replace children = . if children<0 
	gen children_miss = (children<0 | children==.)
	gen anychildren = (children>0) if children!=.
	gen anychildren_nm = (children>0)
	
	* education
	* 3 pid's missing: pid==67 didnt answer which grade, and pid==97 mising altogether (no baseline)
	gen educ = 0 if highest_edu==0
	replace educ = high_edu_class if highest_edu==1
	replace educ = 12+high_edu_grad_years if highest_edu==2
	replace educ = 15 if highest_edu==3
	gen educsq = educ^2		

	* education categorical variable
	gen educ_cat = 0 if educ==0
	replace educ_cat = 1 if educ>=1 & educ<=5
	replace educ_cat = 2 if educ>=6 & educ<=8
	replace educ_cat = 3 if educ>=9 & educ!=.
	replace educ_cat = 4 if educ==.
	gen educ_cat2 = 0 if educ==0
	replace educ_cat2 = 1 if educ>=1 & educ<=4
	replace educ_cat2 = 2 if educ>=5 & educ<=8
	replace educ_cat2 = 3 if educ>=9 & educ!=.
	replace educ_cat2 = 4 if educ==.	

	* literacy
		* read odiya paper
		gen read_odiya_m = read_odiya == 1 if inlist(read_odiya,0,1)
		gen read_odiya_nm = read_odiya_m
			replace read_odiya_nm = 0 if read_odiya_m==.
		gen read_odiya_miss = (read_odiya_m==.)
			replace read_odiya = . if read_odiya_miss==1

	* house construction material /* fully pucca house */ 
		gen richhouse_m = (dwell_type==1) if dwell_type>=0 & dwell_type<.
			
	* primary occupation: casual labor
	gen caslabor_m = (c4a_prime_occup==1| c4a_prime_occup==2) if c4a_prime_occup>=1 & c4a_prime_occup<.
		
	* labor days	
	gen caslabor_miss = (caslabor_m==.)	
	gen caslabor_nm = caslabor_m
		replace caslabor_nm = 0 if caslabor_nm==.
	
	gen bs_labor_7 = c5a_cas_daily_labor if c5a_cas_daily_labor>=0
	gen bs_labor_7_miss = (bs_labor_7==.)
	gen bs_labor_7_nm = bs_labor_7 
		replace bs_labor_7_nm = 0 if bs_labor_7==.
	
	gen bs_labor_30 = c6a_work_others if c6a_work_others>=0
	gen bs_labor_30_miss = (bs_labor_30==.)
	gen bs_labor_30_nm = bs_labor_30
		replace bs_labor_30_nm = 0 if bs_labor_30==.
		
		
*** Wealth proxies

* Own (farm) land
gen ownland_m = (c1_farming==2 | c1_farming==4) if c1_farming>=0 & c1_farming<.
replace ownland_m = 1 if c2b_farm_land_acres>0 & c2b_farm_land_acres<. & c1_farming==-222
replace ownland_m = 1 if c2b_farm_land_guntas>0 & c2b_farm_land_guntas<. & c1_farming==-222

* Can you come up with Rs 1000 in 2 days for emergency medical expenditure
gen canget1k_m = (f4a_emergency_money==1) if f4a_emergency_money>0 & f4a_emergency_money<.

* Any outstanding credit
gen hasnocredits_m = (e4_credit==0) if !mi(e4_credit)

foreach x in ownland canget1k richhouse hasnocredits{
	gen `x' = `x'_m 
	gen `x'_miss = mi(`x') if round<=15
		replace `x'=0 if `x'_miss ==1 & survsample==1
}

*** Indices for wealth ***

* Simple average (if missing vars, compute average without those)
	egen wealth_ind_avg = rowmean(ownland_m canget1k_m richhouse_m hasnocredits_m) if !mi(ownland_m) & !mi(canget1k_m) & !mi(richhouse_m) & !mi(hasnocredits_m)
	egen temp1 = rowmean(ownland_m richhouse_m hasnocredits_m) if !mi(ownland_m) & mi(canget1k_m) & !mi(richhouse_m) & !mi(hasnocredits_m)
	egen temp2 = rowmean(ownland_m canget1k_m hasnocredits_m) if !mi(ownland_m) & !mi(canget1k_m) & mi(richhouse_m) & !mi(hasnocredits_m)
	gen temp3 = hasnocredits_m if mi(ownland_m) & mi(canget1k_m) & mi(richhouse_m) & !mi(hasnocredits_m)
	replace wealth_ind_avg = temp1 if mi(wealth_ind_avg) & !mi(temp1)
	replace wealth_ind_avg = temp2 if mi(wealth_ind_avg) & !mi(temp2)
	replace wealth_ind_avg = temp3 if mi(wealth_ind_avg) & !mi(temp3)	
	drop temp*
	
* Binaries
	qui summ wealth_ind_avg if pid1==1 & round<=15, det
	gen wealth_hi_avg   =  wealth_ind_avg >= r(p50) if wealth_ind_avg != .
	
** LOANS
gen hasloan = (e1_loans==1) if !mi(e1_loans)
	
	* Loan number that should be filled 
	gen hasloan1 = e2_num_loans>=1 if !mi(hasloan)
	gen hasloan2 = e2_num_loans>=2 if !mi(hasloan)
	gen hasloan3 = e2_num_loans>=3 if !mi(hasloan)

* Loan type - moneylender loan	// not missing for survsample
gen loan_moneylender = 0 if !mi(hasloan)
replace loan_moneylender = 1 if hasloan1==1 & e3_2_loan_type1==3
replace loan_moneylender = 1 if hasloan2==1 & e3_2_loan_type2==3
replace loan_moneylender = 1 if hasloan3==1 & e3_2_loan_type3==3

* Loans you are worried about
gen numloans_worry = ( inlist(e3_7_loan_worried1,3,4) + inlist(e3_7_loan_worried2,3,4) + inlist(e3_7_loan_worried3,3,4)) if !mi(hasloan)
gen anyloans_worry = (numloans_worry>0 & numloans_worry<=3) if !mi(hasloan)

* Loans due this year
** Note a lot of these are -555's, which means people dont have a specific due date
** These variables therefore arent super well-defined/interpretable

gen numloans_dueyr0 = (e3_6_yrs_till_dd1==0 |  e3_6_due_date_yr1==year) + (e3_6_yrs_till_dd2==0 |  e3_6_due_date_yr2==year) + (e3_6_yrs_till_dd3==0 |  e3_6_due_date_yr3==year) if !mi(hasloan)
replace numloans_dueyr0 = . if (e3_6_due_date_yr1 < 0 & e3_6_yrs_till_dd1 < 0) & hasloan1==1
replace numloans_dueyr0 = . if (e3_6_due_date_yr2 < 0 & e3_6_yrs_till_dd2 < 0) & hasloan2==1
replace numloans_dueyr0 = . if (e3_6_due_date_yr3 < 0 & e3_6_yrs_till_dd3 < 0) & hasloan3==1
gen numloans_dueyr0_miss = (mi(numloans_dueyr0) & hasloan==1) | mi(hasloan)
gen numloans_dueyr0_nm = numloans_dueyr0 
	replace numloans_dueyr0_nm = 0 if numloans_dueyr0_miss==1

gen anyloans_dueyr0 = (numloans_dueyr0>0) if !mi(numloans_dueyr0)
gen anyloans_dueyr0_nm = anyloans_dueyr0 
	replace anyloans_dueyr0_nm = 0 if numloans_dueyr0_miss==1


* Value of loans and credits
	* Loans
		* amount outstanding among loans you are worried about  
		forvalues i=1/3 {
			gen temp`i' = e3_5_amt_repaid_total`i' if e3_5_amt_repaid_total`i'>=0 & inlist(e3_7_loan_worried`i',3,4)
			replace temp`i'=0 if e3_5_amt_repaid_total`i'>=0 & e3_7_loan_worried`i'<3
		}
		gen bl_loanamt = 0 if hasloan==1
		replace bl_loanamt = temp1 if hasloan1==1
		replace bl_loanamt = temp1 + temp2 if hasloan2==1 
		replace bl_loanamt = temp1 + temp2 + temp3 if hasloan3==1
		
		rename bl_loanamt bl_loanamt_m	
		drop temp*	
			

		gen bl_loanamt_miss = (bl_loanamt_m==.) 
		gen bl_loanamt_nm = bl_loanamt_m
			replace bl_loanamt_nm = 0 if bl_loanamt_m==.

	
* worried about future finances
gen worry1_m = (future_fin_worry==3 | future_fin_worry==4) if !mi(future_fin_worry)	// not asked in R3 and R5
gen worry2_m = future_fin_worry==4 if !mi(future_fin_worry)   

gen future_fin_worry_nm = future_fin_worry
replace future_fin_worry_nm = 5 if future_fin_worry_nm==.
gen future_fin_worry_miss = (future_fin_worry==.)
label var future_fin_worry_nm "More worried"

gen worry1_nm = worry1_m	
replace worry1_nm = 0 if worry1_m==. 

gen worry2_nm = worry2_m
replace worry2_nm = 0 if worry2_m==.
	

// Labels for balance table
* Panel A: Demographics
label var age_m "Age"
label var educ "Years of education" 
label var read_odiya_m "Can read newspaper in Odiya"
label var married "Married"
label var anychildren "Has any children"

* Panel B: Labor and wealth
label var caslabor_m "Primarily daily laborer"
label var bs_labor_7 "Days of paid work in past 7 days"
label var bs_labor_30 "Days of paid work in past 30 days"  
label var wealth_ind_avg  "Wealth index (continuous)"
label var wealth_hi_avg "Higher wealth (binary)"
label var ownland_m "Owns farmland"
label var richhouse_m "House quality (durable house)"
label var hasnocredits_m "No outstanding food loans"
label var canget1k_m "Can get Rs. 1K in emergency"

* Panel C: Financial worries and loans
label var worry2_m "Very worried about finances"
label var worry1_m "Worried about finances"
label var anyloans_worry "Worried about any loan"
label var bl_loanamt_m "Amount of loans worried about"
label var hasloan "Has loans"
label var loan_moneylender "Has moneylender loans"

* Panel D: Baseline productivity and attentiveness
label var attend "Attendance"
label var n_productivity "Hourly production (normalized)"
label var productivity "Hourly production"
label var att_ind "Attentiveness index (continuous)"
label var hiatt_ind "High attentiveness (binary)"

	
***************************************************************** 
*		7. EXPENDITURE 
*****************************************************************
		
/*
// which days should count as after cash? (defined similary for early and late groups, depending on wave)

round 1: last 3 days
round 2-3: group A - last 3 days / group B - last 2 days
round 5-13: group A - last 4 days / group B - last 3 days
round 14-15: group A - last 3 days / group B - last 2 days


gen did_expenditure = !mi(c1_food_tot_exp)
*/
gen postcashdaysago= 4 if inrange(round,5,13) & wavea==1 				// round 5-13
replace postcashdaysago= 3 if inrange(round,5,13) & waveb==1
replace postcashdaysago= 3 if round == 1 								// round 1
replace postcashdaysago= 3 if inlist(round,2,3,14,15) & wavea==1			// round 2,3,14,15
replace postcashdaysago= 2 if inlist(round,2,3,14,15) & waveb==1			

gen postonly2d = postcashdaysago==2

/* variable definitions

// large categories
totalexp: total expenditure
foodexp: all food expenditure
tobacoexp: tobacco, alchohol, marijuana, etc.
loanexp: store credit, loans, lending to others, etc.
medicexp: medical expenditure
agriexp: agricultural expenditures 
	(wages for hired laborers were only added later, so I subtracted this amount to make them consistent; 
	only 3 people paid for hired laborers)
otherexp: other expenditure

// detailed categories
creditpaid: store credit
loanpaid: institutional loan and interest + private loan and interest
lentamount: amount lent to others
cheapnonveg: cheap non-vegetarian such as fish, chicken skin, eggs, etc.
expenonveg: expensive non-vegetarian such as chicken, mutton, etc.
fruit: fruits and vegetables (inconsistent across early vs. late rounds since it excludes potatoes and onions in later rounds)
allveg: fruits and vegetables (similar to above, but consistent across rounds, i.e. potatoes and onions are added back in for later rounds)
tobacco: bidi and chewing tobacco

*/


forval n = 1/4 {
	gen foodexp_`n' = c`n'_food_tot_exp 
	gen tobaccoexp_`n' = c`n'_2_tobacco_tot_exp
	gen loanexp_`n' = c`n'_3_loan_tot_exp
	gen medicexp_`n' = c`n'_4_medi_exp_tot_exp
	gen agriexp_`n' = c`n'_5_agri_input_tot_exp			// included labor wages for round 5-15, but only 3 ppl had such expenses
	gen otherexp_`n' = c`n'_98_oth_tot_exp 
}

forval n = 1/4 {
	gen totalexp_`n' = foodexp_`n' + tobaccoexp_`n' + loanexp_`n' + medicexp_`n' + agriexp_`n' + otherexp_`n'
}


// generate amount variable for detailed categories

forval n = 1/4 {
	gen creditpaid_`n' = c`n'_3_1_store_cred_tot_exp 
	gen loancredit_`n' = c`n'_3_2_institute_tot_exp + c`n'_3_3_pvt_loan_tot_exp + c`n'_3_1_store_cred_tot_exp
}

foreach x of varlist c?_98_oth?_tot_exp{
	replace `x'=. if `x'<0
}

**adding to new/existing categories
	// manually coded these to get at household expenditures, durables, and other categories
	
	// 4 days ago
	gen temp1b = 0
	replace temp1b = c4_98_oth1_tot_exp if !mi(c4_98_oth1_tot_exp) & c4_98_oth1_tot_exp>0 & (regexm(c4_98_oth1_sfy,"CEMENT")) 	// 1b for construction
	
	gen temp1c = 0
	replace temp1c = c4_98_oth1_tot_exp if !mi(c4_98_oth1_tot_exp) & c4_98_oth1_tot_exp>0 & (regexm(c4_98_oth1_sfy,"SAREE") | regexm(c4_98_oth1_sfy,"DRESS") | regexm(c4_98_oth1_sfy,"SHIRT") | regexm(c4_98_oth1_sfy,"PANT") | regexm(c4_98_oth1_sfy,"SANDAL") | regexm(c4_98_oth1_sfy,"BLOUSE") | regexm(c4_98_oth1_sfy,"SHOE") | regexm(c4_98_oth1_sfy,"INNER WEAR") | regexm(c4_98_oth1_sfy,"LUNGI")) // 1c for dress
	
	gen temp1d = 0
	replace temp1d = c4_98_oth1_tot_exp if !mi(c4_98_oth1_tot_exp) & c4_98_oth1_tot_exp>0 & (regexm(c4_98_oth1_sfy,"BOOK") | regexm(c4_98_oth1_sfy,"NOTEBOOK") | regexm(c4_98_oth1_sfy,"NOTE BOOK") | regexm(c4_98_oth1_sfy,"ROUGH NOTE PAD") | regexm(c4_98_oth1_sfy,"SLATE/CHALK") | regexm(c4_98_oth1_sfy,"KHATA/PEN") | regexm(c4_98_oth1_sfy,"NOTE BOOK/PEN") | regexm(c4_98_oth1_sfy,"TUITION") | regexm(c4_98_oth1_sfy,"TUTION")) // 1d for education/stationary
	
	gen temp1e = 0 
	replace temp1e = c4_98_oth1_tot_exp if !mi(c4_98_oth1_tot_exp) & c4_98_oth1_tot_exp>0 & (regexm(c4_98_oth1_sfy,"MOBILE RECHARGE") | regexm(c4_98_oth1_sfy,"DISH TV RECHARGE") | regexm(c4_98_oth1_sfy,"ELECTRIC")) // 1e electricity/mobile/tv 
	
	gen temp1f = 0 
	replace temp1f = c4_98_oth1_tot_exp if !mi(c4_98_oth1_tot_exp) & c4_98_oth1_tot_exp>0 & (regexm(c4_98_oth1_sfy,"SOAP") | regexm(c4_98_oth1_sfy,"BRUSH") | regexm(c4_98_oth1_sfy,"SURF") | regexm(c4_98_oth1_sfy,"COCONUT OIL") | regexm(c4_98_oth1_sfy,"FACE CREAM") | regexm(c4_98_oth1_sfy,"UTENSIL") | regexm(c4_98_oth1_sfy,"SHAMPOO")| regexm(c4_98_oth1_sfy,"PETROL")) // 1f daily utilities 
	
	gen temp1g = 0 
	replace temp1g = c4_98_oth1_tot_exp if !mi(c4_98_oth1_tot_exp) & c4_98_oth1_tot_exp>0 & (regexm(c4_98_oth1_sfy,"AUTO") | regexm(c4_98_oth1_sfy,"FARE")) // 1g transport 	
	
	gen temp1h = 0
	replace temp1h = c4_98_oth1_tot_exp if !mi(c4_98_oth1_tot_exp) & c4_98_oth1_tot_exp>0 & (regexm(c4_98_oth1_sfy,"GIFT")) //gifts, relatives
	
	gen temp1j = 0
	replace temp1j = c4_98_oth1_tot_exp if !mi(c4_98_oth1_tot_exp) & c4_98_oth1_tot_exp>0 & (regexm(c4_98_oth1_sfy,"DAUGHTER") ) // children
	
	gen temp1k = 0
	replace temp1k = c4_98_oth1_tot_exp if !mi(c4_98_oth1_tot_exp) & c4_98_oth1_tot_exp>0 & (regexm(c4_98_oth1_sfy,"CHOCOLATE") | regexm(c4_98_oth1_sfy,"CAKE")) // food
	
	gen temp1m = 0
	replace temp1m = c4_98_oth1_tot_exp if !mi(c4_98_oth1_tot_exp) & c4_98_oth1_tot_exp>0 & (c4_98_oth1_sfy=="MAT") 			// others
		
	gen temp2b = 0
	replace temp2b = c4_98_oth2_tot_exp if !mi(c4_98_oth2_tot_exp) & c4_98_oth2_tot_exp>0 & (regexm(c4_98_oth2_sfy,"BRICK")) 
	
	gen temp2d = 0
	replace temp2d = c4_98_oth2_tot_exp if !mi(c4_98_oth2_tot_exp) & c4_98_oth2_tot_exp>0 & (regexm(c4_98_oth2_sfy,"XEROX"))
	
	gen temp2f = 0
	replace temp2f = c4_98_oth2_tot_exp if !mi(c4_98_oth2_tot_exp) & c4_98_oth2_tot_exp>0 & (regexm(c4_98_oth2_sfy,"SOAP") | regexm(c4_98_oth2_sfy,"PASTE")| regexm(c4_98_oth2_sfy,"SURF")| regexm(c4_98_oth2_sfy,"COCONUT OIL")|regexm(c4_98_oth2_sfy,"PETROL"))
	
	gen temp2h = 0
	replace temp2h = c4_98_oth2_tot_exp if !mi(c4_98_oth2_tot_exp) & c4_98_oth2_tot_exp>0 & regexm(c4_98_oth2_sfy,"RELATIVE")
	
	gen temp3f = 0
	replace temp3f = c4_98_oth3_tot_exp if !mi(c4_98_oth3_tot_exp) & c4_98_oth3_tot_exp>0 & (regexm(c4_98_oth3_sfy,"SOAP")|regexm(c4_98_oth3_sfy,"TONGUE"))
	
	egen check1=rowtotal(temp1*) 				// checking all reports are accounted for
	egen check2=rowtotal(temp2*) 
	egen check3=rowtotal(temp3*) 
	assert check1==c4_98_oth1_tot_exp if !mi(c4_98_oth1_tot_exp) 
	assert check2==c4_98_oth2_tot_exp if !mi(c4_98_oth2_tot_exp)
	assert check3==c4_98_oth3_tot_exp if !mi(c4_98_oth3_tot_exp)
	
	
gen constrexp_4 = temp1b + temp2b 									//construction
gen dressexp_4 = temp1c 										 	//clothes
gen eduexp_4 = temp1d + temp2d 								      	//education, stationary
gen billexp_4 = temp1e 												//electricity, mobile, tv
gen dailyexp_4 = temp1f + temp2f + temp3f 							//daily utilities - soap, oil etc.
gen transpexp_4 = temp1g 											//transportation - auto/fares/bike
gen relativeexp_4 = temp1h + temp2h 								//relatives, gifts
gen childrenexp_4 = temp1j 											//children
gen otherimpexp_4 = temp1m 											//other, misc. 

replace foodexp_4 = foodexp_4 + temp1k								// add to food
drop temp* check*


	// 3 days ago

	gen temp1c = 0
	replace temp1c = c3_98_oth1_tot_exp if !mi(c3_98_oth1_tot_exp) & c3_98_oth1_tot_exp>0 & (regexm(c3_98_oth1_sfy,"SAREE") | regexm(c3_98_oth1_sfy,"DRESS") | regexm(c3_98_oth1_sfy,"SHIRT") | regexm(c3_98_oth1_sfy,"PANT") | regexm(c3_98_oth1_sfy,"SANDAL") | regexm(c3_98_oth1_sfy,"BLOUSE") | regexm(c3_98_oth1_sfy,"SHOE") | regexm(c3_98_oth1_sfy,"INNER WEAR") | regexm(c3_98_oth1_sfy,"LUNGI")) 

	gen temp1d = 0
	replace temp1d = c3_98_oth1_tot_exp if !mi(c3_98_oth1_tot_exp) & c3_98_oth1_tot_exp>0 & (regexm(c3_98_oth1_sfy,"BOOK") | regexm(c3_98_oth1_sfy,"STATIONARY"))

	gen temp1e = 0
	replace temp1e = c3_98_oth1_tot_exp if !mi(c3_98_oth1_tot_exp) & c3_98_oth1_tot_exp>0 & (regexm(c3_98_oth1_sfy,"ELECTRIC") | regexm(c3_98_oth1_sfy,"MOBILE"))

	gen temp1f = 0
	replace temp1f = c3_98_oth1_tot_exp if !mi(c3_98_oth1_tot_exp) & c3_98_oth1_tot_exp>0 & (regexm(c3_98_oth1_sfy,"SOAP") | regexm(c3_98_oth1_sfy,"BROOM")| regexm(c3_98_oth1_sfy,"SURF")| regexm(c3_98_oth1_sfy,"COCONUT OIL")| regexm(c3_98_oth1_sfy,"UTENSIL")| regexm(c3_98_oth1_sfy,"MAT")| regexm(c3_98_oth1_sfy,"PETROL"))

	gen temp1g = 0 
	replace temp1f = c3_98_oth1_tot_exp if !mi(c3_98_oth1_tot_exp) & c3_98_oth1_tot_exp>0 & (regexm(c3_98_oth1_sfy,"BIKE") | regexm(c3_98_oth1_sfy,"AUTO")| regexm(c3_98_oth1_sfy,"TRANSPORT"))

	gen temp1h = 0
	replace temp1h = c3_98_oth1_tot_exp if !mi(c3_98_oth1_tot_exp) & c3_98_oth1_tot_exp>0 & regexm(c3_98_oth1_sfy,"GIFT")

	gen temp1i = 0
	replace temp1i = c3_98_oth1_tot_exp if !mi(c3_98_oth1_tot_exp) & c3_98_oth1_tot_exp>0 & (regexm(c3_98_oth1_sfy,"FESTIVAL") | regexm(c3_98_oth1_sfy,"PUJA") | regexm(c3_98_oth1_sfy,"MARRIAGE")) 

	gen temp1j = 0
	replace temp1j = c3_98_oth1_tot_exp if !mi(c3_98_oth1_tot_exp) & c3_98_oth1_tot_exp>0 & (regexm(c3_98_oth1_sfy,"CHILDREN")) 

	gen temp1k = 0
	replace temp1k = c3_98_oth1_tot_exp if !mi(c3_98_oth1_tot_exp) & c3_98_oth1_tot_exp>0 & (regexm(c3_98_oth1_sfy,"SNACK") | regexm(c3_98_oth1_sfy,"CAKE") | regexm(c3_98_oth1_sfy,"BISCUIT")) // food
	
	gen temp1l = 0
	replace temp1l = c3_98_oth1_tot_exp if !mi(c3_98_oth1_tot_exp) & c3_98_oth1_tot_exp>0 & regexm(c3_98_oth1_sfy,"TRACTOR") // food
	
	gen temp1m = 0
	replace temp1m = c3_98_oth1_tot_exp if !mi(c3_98_oth1_tot_exp) & c3_98_oth1_tot_exp>0 & (regexm(c3_98_oth1_sfy,"STOVE")) 
		

	gen temp2c = 0
	replace temp2c = c3_98_oth2_tot_exp if !mi(c3_98_oth2_tot_exp) & c3_98_oth2_tot_exp>0 & (regexm(c3_98_oth2_sfy,"SAREE") | regexm(c3_98_oth2_sfy,"DRESS") | regexm(c3_98_oth2_sfy,"SHIRT") | regexm(c3_98_oth2_sfy,"PANT") | regexm(c3_98_oth2_sfy,"SANDAL") | regexm(c3_98_oth2_sfy,"BLOUSE") | regexm(c3_98_oth2_sfy,"SHOE") | regexm(c3_98_oth2_sfy,"INNER WEAR") | regexm(c3_98_oth2_sfy,"LUNGI")) 

	gen temp2f = 0
	replace temp2f = c3_98_oth2_tot_exp if !mi(c3_98_oth2_tot_exp) & c3_98_oth2_tot_exp>0 & (regexm(c3_98_oth2_sfy,"SOAP"))

	gen temp2g = 0
	replace temp2f = c3_98_oth2_tot_exp if !mi(c3_98_oth2_tot_exp) & c3_98_oth2_tot_exp>0 & (regexm(c3_98_oth2_sfy,"TRANSPORTING"))
		
	gen temp2k = 0
	replace temp2k = c3_98_oth2_tot_exp if !mi(c3_98_oth2_tot_exp) & c3_98_oth2_tot_exp>0 & (regexm(c3_98_oth2_sfy,"BISCUIT")) // food
	
	
	gen temp3c = 0
	replace temp3c = c3_98_oth3_tot_exp if !mi(c3_98_oth3_tot_exp) & c3_98_oth3_tot_exp>0 & regexm(c3_98_oth3_sfy,"BANIYAN") 


	egen check1=rowtotal(temp1*) 
	egen check2=rowtotal(temp2*) 
	egen check3=rowtotal(temp3*) 
	assert check1==c3_98_oth1_tot_exp if !mi(c3_98_oth1_tot_exp)
	assert check2==c3_98_oth2_tot_exp if !mi(c3_98_oth2_tot_exp)
	assert check3==c3_98_oth3_tot_exp if !mi(c3_98_oth3_tot_exp)

	
gen dressexp_3 = temp1c + temp2c + temp3c 							//clothes
gen eduexp_3 = temp1d 							       				//education, stationary
gen billexp_3 = temp1e 												//electricity, mobile, tv
gen dailyexp_3 = temp1f + temp2f 									//daily utilities - soap, oil etc.
gen transpexp_3 = temp1g+ temp2g 									//transportation - auto/fares/bike
gen relativeexp_3 = temp1h 											//relatives, gifts
gen festexp_3 = temp1i 												//festival, worship
gen childrenexp_3 = temp1j 											//children
gen otherimpexp_3 = temp1m 											//other, misc. 

replace foodexp_3 = foodexp_3 + temp1k + temp2k  					// add to food
replace agriexp_3 = agriexp_3 + temp1l 			  					// add to agriculture

drop temp* check*


	// 2 days ago

	gen temp1b = 0
	replace temp1b = c2_98_oth1_tot_exp if !mi(c2_98_oth1_tot_exp) & c2_98_oth1_tot_exp>0  & regexm(c2_98_oth1_sfy,"WOOD") 

	gen temp1c = 0
	replace temp1c = c2_98_oth1_tot_exp if !mi(c2_98_oth1_tot_exp) & c2_98_oth1_tot_exp>0 & (regexm(c2_98_oth1_sfy,"SAREE") | regexm(c2_98_oth1_sfy,"DRESS") |regexm(c2_98_oth1_sfy,"SHIRT") | regexm(c2_98_oth1_sfy,"PANT") | regexm(c2_98_oth1_sfy,"BLOUSE") | regexm(c2_98_oth1_sfy,"SHOE") | regexm(c2_98_oth1_sfy,"INNER WEAR") | regexm(c2_98_oth1_sfy,"CLOTHES")) 

	gen temp1d = 0
	replace temp1d = c2_98_oth1_tot_exp if !mi(c2_98_oth1_tot_exp) & c2_98_oth1_tot_exp>0 & (regexm(c2_98_oth1_sfy,"BOOK") | regexm(c2_98_oth1_sfy,"PEN") | regexm(c2_98_oth1_sfy,"FEES"))

	gen temp1e = 0
	replace temp1e = c2_98_oth1_tot_exp if !mi(c2_98_oth1_tot_exp) & c2_98_oth1_tot_exp>0 & (regexm(c2_98_oth1_sfy,"ELECTRIC") | regexm(c2_98_oth1_sfy,"MOBILE") | regexm(c2_98_oth1_sfy,"SETUP") | regexm(c2_98_oth1_sfy,"BATTERY"))

	gen temp1f = 0
	replace temp1f = c2_98_oth1_tot_exp if !mi(c2_98_oth1_tot_exp) & c2_98_oth1_tot_exp>0 & (regexm(c2_98_oth1_sfy,"SOAP") | regexm(c2_98_oth1_sfy,"SURF") | regexm(c2_98_oth1_sfy,"SHAMPOO") | regexm(c2_98_oth1_sfy,"OIL")| regexm(c2_98_oth1_sfy,"BUCKET")| regexm(c2_98_oth1_sfy,"SHOP")| regexm(c2_98_oth1_sfy,"PETROL"))

	gen temp1g = 0
	replace temp1g = c2_98_oth1_tot_exp if !mi(c2_98_oth1_tot_exp) & c2_98_oth1_tot_exp>0 & (regexm(c2_98_oth1_sfy,"CYCLE") | regexm(c2_98_oth1_sfy,"AUTO")| regexm(c2_98_oth1_sfy,"GAS") | regexm(c2_98_oth1_sfy,"FARE"))
	
	gen temp1h = 0
	replace temp1h = c2_98_oth1_tot_exp if !mi(c2_98_oth1_tot_exp) & c2_98_oth1_tot_exp>0 & (regexm(c2_98_oth1_sfy,"BROTHER") | regexm(c2_98_oth1_sfy,"MOTHER"))

	gen temp1i = 0
	replace temp1i = c2_98_oth1_tot_exp if !mi(c2_98_oth1_tot_exp) & c2_98_oth1_tot_exp>0 & (regexm(c2_98_oth1_sfy,"FESTIVAL") | regexm(c2_98_oth1_sfy,"WORSHIP")) 

	gen temp1j = 0
	replace temp1j = c2_98_oth1_tot_exp if !mi(c2_98_oth1_tot_exp) & c2_98_oth1_tot_exp>0 & (regexm(c2_98_oth1_sfy,"CHILDREN")) 

	gen temp1k = 0
	replace temp1k = c2_98_oth1_tot_exp if !mi(c2_98_oth1_tot_exp) & c2_98_oth1_tot_exp>0 & (regexm(c2_98_oth1_sfy,"SWEET") | regexm(c2_98_oth1_sfy,"BISCUIT")) 
		

	gen temp2b = 0
	replace temp2b = c2_98_oth2_tot_exp if !mi(c2_98_oth2_tot_exp) & c2_98_oth2_tot_exp>0 & ( regexm(c2_98_oth2_sfy,"CONSTRUCTION")) 

	gen temp2c = 0
	replace temp2c = c2_98_oth2_tot_exp if !mi(c2_98_oth2_tot_exp) & c2_98_oth2_tot_exp>0 & (regexm(c2_98_oth2_sfy,"SAREE") | regexm(c2_98_oth2_sfy,"DRESS") | regexm(c2_98_oth2_sfy,"SHIRT") | regexm(c2_98_oth2_sfy,"PANT") | regexm(c2_98_oth2_sfy,"SANDAL") | regexm(c2_98_oth2_sfy,"BLOUSE") | regexm(c2_98_oth2_sfy,"SHOE") | regexm(c2_98_oth2_sfy,"INNER WEAR") | regexm(c2_98_oth2_sfy,"LUNGI")) 

	gen temp2d = 0
	replace temp2d = c2_98_oth2_tot_exp if !mi(c2_98_oth2_tot_exp) & c2_98_oth2_tot_exp>0 & (regexm(c2_98_oth2_sfy,"PEN"))

	gen temp2e = 0
	replace temp2e = c2_98_oth2_tot_exp if !mi(c2_98_oth2_tot_exp) & c2_98_oth2_tot_exp>0 & (regexm(c2_98_oth2_sfy,"BATTERY") |regexm(c2_98_oth2_sfy,"ELECTRIC"))

	gen temp2f = 0
	replace temp2f = c2_98_oth2_tot_exp if !mi(c2_98_oth2_tot_exp) & c2_98_oth2_tot_exp>0 & (regexm(c2_98_oth2_sfy,"SOAP") | regexm(c2_98_oth2_sfy,"SURF") | regexm(c2_98_oth2_sfy,"SHAMPOO") | regexm(c2_98_oth2_sfy,"OIL")| regexm(c2_98_oth2_sfy,"SAMPOO"))

	gen temp2i = 0
	replace temp2i = c2_98_oth2_tot_exp if !mi(c2_98_oth2_tot_exp) & c2_98_oth2_tot_exp>0 & (regexm(c2_98_oth2_sfy,"FESTIVAL") | regexm(c2_98_oth2_sfy,"PRASAD")) 

	gen temp2j = 0
	replace temp2j = c2_98_oth2_tot_exp if !mi(c2_98_oth2_tot_exp) & c2_98_oth2_tot_exp>0 & (regexm(c2_98_oth2_sfy,"SON")) 

	gen temp2k = 0
	replace temp2k = c2_98_oth2_tot_exp if !mi(c2_98_oth2_tot_exp) & c2_98_oth2_tot_exp>0 & (regexm(c2_98_oth2_sfy,"BISCUIT")) 
	
	gen temp2m = 0
	replace temp2m = c2_98_oth2_tot_exp if !mi(c2_98_oth2_tot_exp) & c2_98_oth2_tot_exp>0 & (regexm(c2_98_oth2_sfy,"TIN BOX")) 
	

	gen temp3f = 0
	replace temp3f = c2_98_oth3_tot_exp if !mi(c2_98_oth3_tot_exp) & c2_98_oth3_tot_exp>0 & (regexm(c2_98_oth3_sfy,"SOAP") | regexm(c2_98_oth3_sfy,"SURF"))
	
	gen temp3g = 0
	replace temp3g = c2_98_oth3_tot_exp if !mi(c2_98_oth3_tot_exp) & c2_98_oth3_tot_exp>0 & (regexm(c2_98_oth3_sfy,"FARE"))
	
	gen temp3m = 0
	replace temp3m = c2_98_oth3_tot_exp if !mi(c2_98_oth3_tot_exp) & c2_98_oth3_tot_exp>0 & (regexm(c2_98_oth3_sfy,"SCOTT"))

	egen check1=rowtotal(temp1*) 
	egen check2=rowtotal(temp2*) 
	egen check3=rowtotal(temp3*) 
	assert check1==c2_98_oth1_tot_exp if !mi(c2_98_oth1_tot_exp)
	assert check2==c2_98_oth2_tot_exp if !mi(c2_98_oth2_tot_exp)
	assert check3==c2_98_oth3_tot_exp if !mi(c2_98_oth3_tot_exp)

	
gen constrexp_2 = temp1b + temp2b 											//construction
gen dressexp_2 = temp1c + temp2c 											//clothes
gen eduexp_2 = temp1d + temp2d 												//education, stationary
gen billexp_2 = temp1e + temp2e 											//electricity, mobile, tv
gen dailyexp_2 = temp1f + temp2f + temp3f  									//daily utilities - soap, oil etc.
gen transpexp_2 = temp1g + temp3g 											//transportation - auto/fares/bike
gen relativeexp_2 = temp1h 													//relatives, gifts
gen festexp_2 = temp1i + temp2i 											//festival, worship
gen childrenexp_2 = temp1j + temp2j											//children
gen otherimpexp_2 = temp2m + temp3m											//other, misc. 

replace foodexp_2 = foodexp_2 + temp1k + temp2k  					// add to food
drop temp* check*



	// 1 day ago

	gen temp1b = 0
	replace temp1b = c1_98_oth1_tot_exp if !mi(c1_98_oth1_tot_exp) & c1_98_oth1_tot_exp>0 & (regexm(c1_98_oth1_sfy,"BRICKS") | regexm(c1_98_oth1_sfy,"BAMBOO") )

	gen temp1c = 0
	replace temp1c = c1_98_oth1_tot_exp if !mi(c1_98_oth1_tot_exp) & c1_98_oth1_tot_exp>0 & (regexm(c1_98_oth1_sfy,"SAREE") | regexm(c1_98_oth1_sfy,"DRESS") | regexm(c1_98_oth1_sfy,"SHIRT") | regexm(c1_98_oth1_sfy,"PANT") | regexm(c1_98_oth1_sfy,"SANDAL") | regexm(c1_98_oth1_sfy,"BLOUSE") | regexm(c1_98_oth1_sfy,"SHOE") | regexm(c1_98_oth1_sfy,"IMITATION ORNAMENTS") | regexm(c1_98_oth1_sfy,"BANGLE")) 

	gen temp1d = 0
	replace temp1d = c1_98_oth1_tot_exp if !mi(c1_98_oth1_tot_exp) & c1_98_oth1_tot_exp>0 & (c1_98_oth1_sfy=="PEN" | regexm(c1_98_oth1_sfy,"COLOUR PEN") | regexm(c1_98_oth1_sfy,"BOOK") |  regexm(c1_98_oth1_sfy,"STUDY")| regexm(c1_98_oth1_sfy,"HOSTEL"))

	gen temp1e = 0
	replace temp1e = c1_98_oth1_tot_exp if !mi(c1_98_oth1_tot_exp) & c1_98_oth1_tot_exp>0 & (regexm(c1_98_oth1_sfy,"MOBILE") | regexm(c1_98_oth1_sfy,"ELECTRIC") | regexm(c1_98_oth1_sfy,"TV") | regexm(c1_98_oth1_sfy,"KIROSIN"))

	gen temp1f = 0
	replace temp1f = c1_98_oth1_tot_exp if !mi(c1_98_oth1_tot_exp) & c1_98_oth1_tot_exp>0 & (regexm(c1_98_oth1_sfy,"SHOP") | regexm(c1_98_oth1_sfy,"SOAP") |regexm(c1_98_oth1_sfy,"COCONUT") |regexm(c1_98_oth1_sfy,"SURF")|regexm(c1_98_oth1_sfy,"SHAMPOO")|regexm(c1_98_oth1_sfy,"UTENSIL") | regexm(c1_98_oth1_sfy,"PETROL")|regexm(c1_98_oth1_sfy,"DIESEL"))

	gen temp1g = 0
	replace temp1g = c1_98_oth1_tot_exp if !mi(c1_98_oth1_tot_exp) & c1_98_oth1_tot_exp>0 & (regexm(c1_98_oth1_sfy,"AUTO") |regexm(c1_98_oth1_sfy,"CYCLE"))
	
	gen temp1h = 0
	replace temp1h = c1_98_oth1_tot_exp if !mi(c1_98_oth1_tot_exp) & c1_98_oth1_tot_exp>0 & (regexm(c1_98_oth1_sfy,"MOTHER") | regexm(c1_98_oth1_sfy,"BROTHER") | regexm(c1_98_oth1_sfy,"RELATIVE"))
	
	gen temp1i = 0
	replace temp1i = c1_98_oth1_tot_exp if !mi(c1_98_oth1_tot_exp) & c1_98_oth1_tot_exp>0 & (regexm(c1_98_oth1_sfy,"FESTIVAL") | regexm(c1_98_oth1_sfy,"INCENSE")| regexm(c1_98_oth1_sfy,"PRASAD"))
	
	gen temp1j = 0
	replace temp1j = c1_98_oth1_tot_exp if !mi(c1_98_oth1_tot_exp) & c1_98_oth1_tot_exp>0 & (regexm(c1_98_oth1_sfy,"GIVE TO THE CHILD") | regexm(c1_98_oth1_sfy,"FOR CHILDRENS EXPENSES"))

	gen temp1k = 0
	replace temp1k = c1_98_oth1_tot_exp if !mi(c1_98_oth1_tot_exp) & c1_98_oth1_tot_exp>0 & (regexm(c1_98_oth1_sfy,"CAKE")| regexm(c1_98_oth1_sfy,"BISCUIT")| regexm(c1_98_oth1_sfy,"WHEAT")| regexm(c1_98_oth1_sfy,"MILK"))

	gen temp1l = 0
	replace temp1l = c1_98_oth1_tot_exp if !mi(c1_98_oth1_tot_exp) & c1_98_oth1_tot_exp>0 & (regexm(c1_98_oth1_sfy,"TRACTOR"))
	
	gen temp1m = 0
	replace temp1m = c1_98_oth1_tot_exp if !mi(c1_98_oth1_tot_exp) & c1_98_oth1_tot_exp>0 & (regexm(c1_98_oth1_sfy,"VISIT TO DHENKANAL"))
	

	gen temp2b = 0
	replace temp2b = c1_98_oth2_tot_exp if !mi(c1_98_oth2_tot_exp) & c1_98_oth2_tot_exp>0 & (regexm(c1_98_oth2_sfy,"WAGE")) 

	gen temp2c = 0
	replace temp2c = c1_98_oth2_tot_exp if !mi(c1_98_oth2_tot_exp) & c1_98_oth2_tot_exp>0 & (regexm(c1_98_oth2_sfy,"SAREE") |regexm(c1_98_oth2_sfy,"DRESS")|regexm(c1_98_oth2_sfy,"SHIRT") | regexm(c1_98_oth2_sfy,"PANT") | regexm(c1_98_oth2_sfy,"SANDAL") | regexm(c1_98_oth2_sfy,"BLOUSE") | regexm(c1_98_oth2_sfy,"SHOE") | regexm(c1_98_oth2_sfy,"INNER WEAR") | regexm(c1_98_oth2_sfy,"LUNGI")) 
	
	gen temp2e = 0
	replace temp2e = c1_98_oth2_tot_exp if !mi(c1_98_oth2_tot_exp) & c1_98_oth2_tot_exp>0 & (regexm(c1_98_oth2_sfy,"MOBILE")|regexm(c1_98_oth2_sfy,"WIRING"))
	
	gen temp2f = 0
	replace temp2f = c1_98_oth2_tot_exp if !mi(c1_98_oth2_tot_exp) & c1_98_oth2_tot_exp>0 & (regexm(c1_98_oth2_sfy,"SOAP")|regexm(c1_98_oth2_sfy,"COCONUT") |regexm(c1_98_oth2_sfy,"SURF")|regexm(c1_98_oth2_sfy,"SHAMPOO")|regexm(c1_98_oth2_sfy,"BRUSH")|regexm(c1_98_oth2_sfy,"OIL")|regexm(c1_98_oth2_sfy,"PASTE")) & !regexm(c1_98_oth2_sfy,"WIRING")
	
	gen temp2h = 0
	replace temp2h = c1_98_oth2_tot_exp if !mi(c1_98_oth2_tot_exp) & c1_98_oth2_tot_exp>0 & (regexm(c1_98_oth2_sfy,"RELATIVE")) 
	
	gen temp3f = 0
	replace temp3f = c1_98_oth3_tot_exp if !mi(c1_98_oth3_tot_exp) & c1_98_oth3_tot_exp>0 & (regexm(c1_98_oth3_sfy,"PASTE")|regexm(c1_98_oth3_sfy,"COCONUT") |regexm(c1_98_oth3_sfy,"SAMPOO"))

	egen check1=rowtotal(temp1*) 
	egen check2=rowtotal(temp2*) 
	egen check3=rowtotal(temp3*) 
	assert check1==c1_98_oth1_tot_exp if !mi(c1_98_oth1_tot_exp)
	assert check2==c1_98_oth2_tot_exp if !mi(c1_98_oth2_tot_exp)
	assert check3==c1_98_oth3_tot_exp if !mi(c1_98_oth3_tot_exp)
	
	
gen constrexp_1 = temp1b + temp2b 											//construction
gen dressexp_1 = temp1c + temp2c 											//clothes
gen eduexp_1 = temp1d 														//education, stationary
gen billexp_1 = temp1e + temp2e 											//electricity, mobile, tv
gen dailyexp_1 = temp1f + temp2f + temp3f 									//daily utilities - soap, oil etc.
gen transpexp_1 = temp1g 													//transportation - auto/fares/bike
gen relativeexp_1 = temp1h + temp2h 										//relatives, gifts
gen festexp_1 = temp1i 												 		//festival, worship
gen childrenexp_1 = temp1j 													//children
gen otherimpexp_1 = temp1m 													//other, misc. 

replace foodexp_1 = foodexp_1 + temp1k 					 					// add to food
replace agriexp_1 = agriexp_1 + temp1l 			  							// add to agriculture

drop temp* check*


*** Durable purchases

gen temp1 = 0 
replace temp1 = c1_5_4_oth_tot_exp if regexm(c1_5_4_oth_sfy,"HOE") | regexm(c1_5_4_oth_sfy,"PLOUGH")
gen durable_1 = temp1 + c1_5_1_heavy_input_tot_exp

gen temp2 = 0
replace temp2 = c2_5_4_oth_tot_exp if regexm(c2_5_4_oth_sfy,"HOE") | regexm(c2_5_4_oth_sfy,"SPADE")
gen durable_2 = temp2 + c2_5_1_heavy_input_tot_exp

gen temp3 = 0
replace temp3 = c3_5_4_oth_tot_exp if regexm(c3_5_4_oth_sfy,"PLOUGH")
gen durable_3 = temp2 + c3_5_1_heavy_input_tot_exp

gen durable_4 = c4_5_1_heavy_input_tot_exp
drop temp*


*** Expenses taken on credit

local k=1
foreach x in food_new_loan tobacco_new_loan loan_new_loan medi_exp_new_loan agri_input_new_loan oth_new_loan{
	
	if "`x'"=="food_new_loan"{
		forval i=1/4{
			gen `x'_`i' = c`i'_`x' if c`i'_`x'>=0
			replace `x'_`i' = 0 if mi(`x'_`i') & !mi(c`i'_`x')
		}	
	}
	else if "`x'"=="oth_new_loan"{
		forval i=1/4{
			gen `x'_`i' = c`i'_98_`x' if c`i'_98_`x'>=0
			replace `x'_`i' = 0 if mi(`x'_`i') & !mi(c`i'_98_`x')
		}		
	} 
	else{
		forval i=1/4{
			gen `x'_`i' = c`i'_`k'_`x' if c`i'_`k'_`x'>=0
			replace `x'_`i' = 0 if mi(`x'_`i') & !mi(c`i'_`k'_`x')
		}		
	} 
	
	local `k++'
}

forval i=1/4{
	egen spending_new_loan_`i'=rowtotal(food_new_loan_`i' tobacco_new_loan_`i' loan_new_loan_`i' medi_exp_new_loan_`i' agri_input_new_loan_`i' oth_new_loan_`i'), m
}


*** Borrowing at worksite

gen lent = d1a_onsite_loan==1 if !mi(d1a_onsite_loan)
gen borrow = d1a_onsite_loan==2 if !mi(d1a_onsite_loan)


***************************************************************** 
*		8. NUTRITION
*****************************************************************



* last full hour slot of the day			: FIXED
	gen l1fs = (Hour_Start==1) if round>3 & round<=15 
		replace l1fs = (slot==7) if (round==1 | round==3)
		replace l1fs = (slot==7) if round==2 
		// alternate way: 
		gen temp = slot if halfslot==0 & lastslot20==0
		egen temp2 = max(temp), by(round pid day)
		gen temp3=slot==temp2 if round<=15
		assert temp3==l1fs
		drop temp*
	* second to last full hour slot of the day
	gen l2fs = (Hour_Start==12) if round>3 & round<=15
		replace l2fs = (slot==6) if (round==1 | round==3)
		replace l2fs = (slot==6) if round==2 
	* first or second last full slots of the day
	gen l12fs = l1fs + l2fs


// had breakfast in the 2 days after cash (average)

gen bf2d_avg = (c3_6_a_breakfast + c2_6_a_breakfast)/2 if postcashdaysago==4
replace bf2d_avg = (c2_6_a_breakfast + c1_6_a_breakfast)/2 if postcashdaysago==3
replace bf2d_avg = c1_6_a_breakfast if postcashdaysago==2
	

// rice quantity 

forval i=1/4{
	gen riceq`i' = c`i'_6_breakfast_qty1 if c`i'_6_breakfast_item1==1 & c`i'_6_breakfast_unit1=="GRAM" 
	replace riceq`i' = c`i'_6_breakfast_qty2 if c`i'_6_breakfast_item2==1 & c`i'_6_breakfast_unit2=="GRAM" & mi(riceq`i')
	replace riceq`i' = c`i'_6_breakfast_qty3 if c`i'_6_breakfast_item3==1 & c`i'_6_breakfast_unit3=="GRAM" & mi(riceq`i')
	replace riceq`i' = 0 if mi(riceq`i') & !mi(c`i'_6_a_breakfast)
	
	forval j=1/16{
		gen ateitem`j'_`i' = 0 if !mi(c`i'_6_a_breakfast)
		replace ateitem`j'_`i'= 1 if c`i'_6_breakfast_item1==`j' | c`i'_6_breakfast_item2==`j' | c`i'_6_breakfast_item3==`j' |c`i'_6_breakfast_item4==`j' | c`i'_6_breakfast_item5==`j'
	}
}

forval j=1/16{
	gen ateitem`j' = ateitem`j'_3 | ateitem`j'_2 if !mi(ateitem`j'_3) & !mi(ateitem`j'_2) & postcashdaysago==4
	replace ateitem`j' = ateitem`j'_2 | ateitem`j'_1 if !mi(ateitem`j'_2) & !mi(ateitem`j'_1) & postcashdaysago==3
	replace ateitem`j' = ateitem`j'_1 if !mi(ateitem`j'_1) & postcashdaysago==2
}

// rice quantity in 2 days after cash (average)

gen rice2dq = (riceq3 + riceq2)/2 if postcashdaysago==4
replace rice2dq = (riceq2 + riceq1)/2 if postcashdaysago==3
replace rice2dq = riceq1 if postcashdaysago==2

// had rice in the 2 days after cash (average)

gen rice2d_avg = (ateitem1_3 + ateitem1_2)/2 if !mi(ateitem1_3) & !mi(ateitem1_2) & postcashdaysago==4
replace rice2d_avg = (ateitem1_2 + ateitem1_1)/2 if !mi(ateitem1_2) & !mi(ateitem1_1) & postcashdaysago==3
replace rice2d_avg = ateitem1_1 if !mi(ateitem1_1) & postcashdaysago==2


// had veg in the 2 days after cash (average)

gen veg2d_avg = (ateitem3_3 + ateitem3_2)/2 if !mi(ateitem3_3) & !mi(ateitem3_2) & postcashdaysago==4
replace veg2d_avg = (ateitem3_2 + ateitem3_1)/2 if !mi(ateitem3_2) & !mi(ateitem3_1) & postcashdaysago==3
replace veg2d_avg = ateitem3_1 if !mi(ateitem3_1) & postcashdaysago==2


// had something other than rice and veg in the 2 days after cash (average)

forval i=1/4{
	egen ateoth_`i'=rowmax(ateitem2_`i' ateitem4_`i' ateitem5_`i' ateitem6_`i' ateitem7_`i' ateitem8_`i' ateitem9_`i' ateitem10_`i' ateitem11_`i' ateitem12_`i' ateitem13_`i' ateitem14_`i' ateitem15_`i' ateitem16_`i')
}

gen other2d_avg = (ateoth_3 + ateoth_2)/2 if !mi(ateoth_3) & !mi(ateoth_2) & postcashdaysago==4
replace other2d_avg = (ateoth_2 + ateoth_1)/2 if !mi(ateoth_2) & !mi(ateoth_1) & postcashdaysago==3
replace other2d_avg = ateoth_1 if !mi(ateoth_1) & postcashdaysago==2



label var bf2d_avg "Had breakfast (2 days avg)"
label var rice2dq "Amount of rice (2 days avg)"
label var rice2d_avg "Had rice (2 days avg)"
label var veg2d_avg "Had veg (2 days avg)"
label var other2d_avg "Had other food (2 days avg)"		
		
		
***************************************************************** 
*		9. EXIT SURVEY
*****************************************************************


*** Top of mind Picture A
gen pic1rsn_oth = (c1b_top_of_mind1==-98 | c1b_top_of_mind2==-98 | c1b_top_of_mind3==-98 | c1b_top_of_mind4==-98 | c1b_top_of_mind5==-98 | c1b_top_of_mind5==98) if c1b_top_of_mind1!=.
gen pic1rsn_poor = (c1b_top_of_mind1==1 | c1b_top_of_mind2==1 | c1b_top_of_mind3==1 | c1b_top_of_mind4==1 | c1b_top_of_mind5==1) if c1b_top_of_mind1!=.
gen pic1rsn_sick = (c1b_top_of_mind1==5 | c1b_top_of_mind2==5 | c1b_top_of_mind3==5 | c1b_top_of_mind4==5 | c1b_top_of_mind5==5) if c1b_top_of_mind1!=.

gen pic1rsn_noworry = (pic1rsn_sick==1 | pic1rsn_poor==1 ) if c1b_top_of_mind1!=.
gen pic1rsn_nopoverty = (pic1rsn_sick==1 ) if c1b_top_of_mind1!=.

gen pic1rsn_oth_worry = regexm(c1b_top_of_mind_oth,"WORRY") | regexm(c1b_top_of_mind_oth,"WORRI") 
gen pic1rsn_oth_loan = (regexm(c1b_top_of_mind_oth,"LOAN") | regexm(c1b_top_of_mind_oth,"BORROW") | regexm(c1b_top_of_mind_oth,"DEBT")) & !regexm(c1b_top_of_mind_oth,"WORRY") & !regexm(c1b_top_of_mind_oth,"WORRI")
gen pic1rsn_oth_job = (regexm(c1b_top_of_mind_oth,"WORK") | regexm(c1b_top_of_mind_oth,"JOB")) & !regexm(c1b_top_of_mind_oth,"WORRY") & !regexm(c1b_top_of_mind_oth,"WORRI")
gen pic1rsn_oth_marriage = regexm(c1b_top_of_mind_oth,"MARRIAGE") & !regexm(c1b_top_of_mind_oth,"WORRY") & !regexm(c1b_top_of_mind_oth,"WORRI")
gen pic1rsn_oth_poverty = (regexm(c1b_top_of_mind_oth,"EXPENSE") | regexm(c1b_top_of_mind_oth,"HOUSE") | regexm(c1b_top_of_mind_oth,"LOSS")) & !regexm(c1b_top_of_mind_oth,"WORRY") & !regexm(c1b_top_of_mind_oth,"WORRI")
gen pic1rsn_oth_sick = regexm(c1b_top_of_mind_oth," ILL") | regexm(c1b_top_of_mind_oth,"SICK") & !regexm(c1b_top_of_mind_oth,"WORRY") & !regexm(c1b_top_of_mind_oth,"WORRI")
gen pic1rsn_oth_other = !mi(c1b_top_of_mind_oth) & c1b_top_of_mind_oth!="-555" & pic1rsn_oth_worry==0 & pic1rsn_oth_loan==0 & pic1rsn_oth_job==0 & pic1rsn_oth_marriage==0 & pic1rsn_oth_poverty==0 & pic1rsn_oth_sick==0


** mention sth other than worry , mention sth other than worry or poverty
gen pic1rsn_noworry_v3 = pic1rsn_noworry		
replace pic1rsn_noworry_v3 = 1 if pic1rsn_oth==1 & (pic1rsn_oth_sick==1 | pic1rsn_oth_other==1 | pic1rsn_oth_poverty==1)

gen pic1rsn_nopoverty_v2 = pic1rsn_nopoverty
replace pic1rsn_nopoverty_v2 = 1 if pic1rsn_oth==1 & (pic1rsn_oth_sick==1 | pic1rsn_oth_other==1) 


** Happiness scale
gen happy1 = (happiness_scale==1 | happiness_scale==2) if happiness_scale!=.
gen happy2 = (happiness_scale==1) if happiness_scale!=.


** Sleep
gen sleep_hours = c5a_sleep_hrs if c5a_sleep_hrs>=0 & !mi(c5a_sleep_hrs)
gen sleep_scale = c5b_sleep_well if c5b_sleep_well>0 & c5b_sleep_well<.
gen sleep_well = c5b_sleep_well==1 if c5b_sleep_well>0 & c5b_sleep_well<.

** Labels
label var happiness_scale "Happiness scale"
label var happy1 "Happy or very happy"
label var happy2 "Very happy"


		
***************************************************************** 
*		10. OTHER VARIABLES
*****************************************************************


********** END OF DAY SURVEY *************

* Q1a - what did you think about today
gen thinkfin = (q1a_think_work_today1==2 | q1a_think_work_today2==2 | q1a_think_work_today3==2 | q1a_think_work_today4==2) if q1a_think_work_today1!=.
gen thinktask = (q1a_think_work_today1==3 | q1a_think_work_today2==3 | q1a_think_work_today3==3 | q1a_think_work_today4==3) if q1a_think_work_today1!=.
gen thinkhh = (q1a_think_work_today1==1 | q1a_think_work_today2==1 | q1a_think_work_today3==1 | q1a_think_work_today4==1) if q1a_think_work_today1!=.

* think about hh worries or finances
gen thinkanyworry = (thinkhh==1 | thinkfin==1) if thinkfin!=.

* only thought about work not worries
gen thinkonlytask = (thinktask==1 & thinkanyworry==0) if !mi(thinktask)


		
****************** USED WORKSITE ******************************

* order of round in worksite
gen numround = 1 if inlist(round,1,3,5,7,12)
replace numround = 2 if inlist(round,2,6,9,14)
replace numround = 3 if inlist(round,8,11)
replace numround = 4 if inlist(round,10,13)
replace numround = 5 if inlist(round,15)

gen prevw1 = numround!=1 
gen prevw2 = numround

egen totround = max(numround), by(worksite_id)


************ DAILY TOTAL WORKING TIME

// mark first production slot in each day
gen temp2 = 1 if Total_Production >= 0
sort round temp2 pid day slot
by round temp2 pid day: gen temp3 = _n==1		
replace temp3 = 0 if temp2!=1
gen temp4 = slot==2 & temp3==1		// this is slot 1 except in round 1 day 2
egen temp5 = max(temp4) if round<=15, by(pid day)

// code up start time
gen temp6 = Hour_Start if Hour_Start>7
replace temp6 = Hour_Start + 12 if Hour_Start<7
replace temp6 = temp6 + Min_Start/60

gen temp7 = temp6 if temp3==1
egen temp8 = max(temp7) if round<=15, by(pid day)

// double checking code
gen temp9 = temp6 if slot==1 & !(round==1 & day==2) & attend==1
replace temp9 = temp6 if slot==2 & round==1 & day==2 & attend==1
egen temp10= max(temp9) if round<=15, by(pid day)
assert temp8==temp10

rename temp8 starttime
drop temp*


// mark last production slot in each day
gen temp2 = 1 if Total_Production >= 0
sort round temp2 pid day slot
by round temp2 pid day: gen temp3 = _n==_N
replace temp3 = 0 if temp2!=1

gen temp4 = slot if temp3==1		
egen temp5 = max(temp4), by(round pid day)
replace temp5=. if temp2!=1
egen temp6 = max(temp4) if round<=15, by(round day)
replace temp6=. if temp2!=1			// endtime differs in round 1 day 8

// code up end time
gen temp7 = Hour_End if Hour_End>7
replace temp7 = Hour_End + 12 if Hour_End<7
replace temp7 = temp7 + Min_End/60
gen temp8 = temp7 if temp3==1
egen temp9 = max(temp8) if round<=15, by(pid day)

rename temp9 endtime
drop temp*

gen workingtime = endtime - starttime
label var workingtime "Number of hours worked in a day"

save "$dir/data/analysis_all.dta", replace



***************************************************************** 
*		11. BASELINE PROD AND ATTENTION VARS
*****************************************************************	

use "$dir/data/analysis_all.dta", clear
keep if round<=15	

******* PRODUCTIVITY **********

* Baseline productivity		

	***** PREFERRED BASELINE MEASURE: LAST DAY OF OBS IN YOUR BASELINE PERIOD
	* last day of your baseline period
	egen temp = max(day) if pre_announce==1 & attend==1, by(pid)
	egen lastblday = max(temp), by(pid)
		drop temp
		
	egen temp = mean(n_productivity) if day==lastblday, by(pid)
	egen blhrprod = max(temp), by(pid)
	gen blhrprod_miss = (blhrprod==.)
		replace blhrprod = 0 if blhrprod==.
	gen blhrprodsq = blhrprod^2
	drop temp
		

******* ATTENTIVENESS **********

	* last day of obs in your baseline period 
	* att_ind
	egen temp = mean(att_ind) if day==lastblday, by(pid)
	egen blatt = max(temp), by(pid)
	gen blatt_miss = (blatt==.)
		replace blatt = 0 if blatt==.
	gen blattsq = blatt^2
	drop temp
	
	* hiatt_ind
	egen temp = mean(hiatt_ind) if day==lastblday, by(pid)
	egen blhiatt = max(temp), by(pid)
	gen blhiatt_miss = (blhiatt==.)
		replace blhiatt = 0 if blhiatt==.
	drop temp
	
	* att_ind_pca
	egen temp = mean(att_ind_pca) if day==lastblday, by(pid)
	egen blattpca = max(temp), by(pid)
	gen blattpca_miss = (blattpca==.)
		replace blattpca = 0 if blattpca==.
	drop temp

	**********************************************
	**** 3 obs/worker: baseline, announcement and post
	**********************************************
	
	* 3 pds
	gen pd = 0 if post_announce==0 & day>3
		replace pd = 0 if round==14 & day==3 & group==1

	replace pd = 1 if papc==1
	replace pd = 2 if postcash==1

	* output
	egen temp1 = mean(n_productivity) if disrupt==0, by(pid pd)
	egen temp2 = max(temp1), by(pid pd)
	sort pid pd day slot
	by pid pd: gen hrprodpd=temp2 if _n==1
	drop temp*

	* interactions
	gen cash_announce = cash*(pd==1)
	gen cash_postpay = cash*(pd==2)
	label var cash "Cash"
	label var cash_postpay "Cash $\times$ Post-pay" 	
	label var cash_announce "Cash $\times$ Announcement period" 

	
***************************************************************** 
*		12. WEALTH HETEROGENEITY + SAVE ALL ROUNDS
*****************************************************************


	cap drop *_hiw
	foreach y of varlist payday payday_cash payday_wb payday_cash_wb papc papc_cash gotcash postcash cash_postpay  ///
		pre_announce_2_cash pre_announce_1_cash post_announce_1_cash pre_announce_2 pre_announce_1 post_announce_1 post_announce_2_cash post_announce_3p_cash post_announce_2 post_announce_3p{
		gen `y'_hiw = wealth_hi_avg*`y'
	} 


	label var pre_announce_2_cash_hiw "Announce cash $\times$ Pre 2 days $\times$ Higher wealth"
	label var pre_announce_1_cash_hiw "Announce cash $\times$ Pre 1 day $\times$ Higher wealth"
	label var post_announce_1_cash_hiw "Cash $\times$ 1 day post announcement $\times$ Higher wealth"
	label var post_announce_2_cash_hiw "Cash $\times$ 2 days post announcement $\times$ Higher wealth"
	label var post_announce_3p_cash_hiw "Announce cash $\times$ Post 3+ days $\times$ Higher wealth"
	label var payday_cash "Cash $\times$ Payment day"
	label var gotcash_hiw "Cash $\times$ Post-pay $\times$ Higher wealth" 
	label var payday_cash_hiw "Cash $\times$ Payment day $\times$ Higher wealth" 
	label var payday_cash_wb_hiw "Cash $\times$ Payment day $\times$ Wave B $\times$ Higher wealth "  
	label var payday_wb_hiw "Wave B $\times$ Payment day $\times$ Higher wealth"



***************************************************************** 
*		13. LASSO
*****************************************************************
		
		*RT: Check these against original after matching previous code
************* VARIABLE NORMALIZATIONS FOR LASSO **************
	
	// prod and attention
	foreach var of varlist blhrprod blhrprodsq blhrprod_miss blatt blattsq blatt_miss blhiatt blhiatt_miss blattpca blattpca_miss {
		quietly summarize `var' if round<=15
		generate `var'Z = (`var'-r(mean)) / r(sd) if round<=15
	}
	
	
	// survey vars without missing vaues
	foreach var of varlist educ educsq married widowed divorced exper expersq wealth_hi_avg wealth_ind_avg hasnocredits hasloan loan_moneylender anyloans_worry{
		quietly summarize `var' if round<=15
		generate `var'Z = (`var'-r(mean)) / r(sd) if round<=15
	}
	
	// survey vars with missing values:
	foreach var of varlist richhouse richhouse_miss canget1k canget1k_miss ownland ownland_miss age_nm age_miss read_odiya_nm read_odiya_miss anychildren_nm children_miss caslabor_nm caslabor_miss bs_labor_7_nm bs_labor_7_miss bs_labor_30_nm bs_labor_30_miss future_fin_worry_nm worry1_nm worry2_nm future_fin_worry_miss bl_loanamt_nm bl_loanamt_miss anyloans_dueyr0_nm numloans_dueyr0_nm numloans_dueyr0_miss{
		quietly summarize `var' if round<=15
		generate `var'Z = (`var'-r(mean)) / r(sd) if round<=15
	}
	


**** PDS lasso reg 

	pdslasso n_productivity cash_postpay cash_announce (i.roundgroup i.pd blhrprodZ blhrprodsqZ blhrprod_missZ blattZ blattsqZ blatt_missZ educZ educsqZ experZ expersqZ marriedZ widowedZ divorcedZ wealth_hi_avgZ wealth_ind_avgZ richhouseZ richhouse_missZ canget1kZ canget1k_missZ hasnocreditsZ ownlandZ ownland_missZ age_nmZ age_missZ read_odiya_nmZ read_odiya_missZ anychildren_nmZ children_missZ caslabor_nmZ caslabor_missZ bs_labor_7_nmZ bs_labor_7_missZ bs_labor_30_nmZ bs_labor_30_missZ future_fin_worry_nmZ worry1_nmZ future_fin_worry_missZ hasloanZ loan_moneylenderZ anyloans_worryZ anyloans_dueyr0_nmZ numloans_dueyr0_nmZ numloans_dueyr0_missZ bl_loanamt_nmZ bl_loanamt_missZ) if pd>0 , cl(pid) pnotpen(i.roundgroup i.pd)
		* controls chosen: blhrprod blhrprodsq blhrprod_miss bs_labor_30_miss future_fin_worry_miss
		reghdfe n_productivity cash_postpay cash_announce blhrprod blhrprodsq blhrprod_miss bs_labor_30_miss future_fin_worry_miss   i.pd if pd>0 , vce(cl pid) absorb(roundgroup)
		
	pdslasso att_ind cash_postpay cash_announce (i.roundgroup i.pd blhrprodZ blhrprodsqZ blhrprod_missZ blattZ blattsqZ blatt_missZ educZ educsqZ experZ expersqZ marriedZ widowedZ divorcedZ wealth_hi_avgZ wealth_ind_avgZ richhouseZ richhouse_missZ canget1kZ canget1k_missZ hasnocreditsZ ownlandZ ownland_missZ age_nmZ age_missZ read_odiya_nmZ read_odiya_missZ anychildren_nmZ children_missZ caslabor_nmZ caslabor_missZ bs_labor_7_nmZ bs_labor_7_missZ bs_labor_30_nmZ bs_labor_30_missZ future_fin_worry_nmZ worry1_nmZ future_fin_worry_missZ hasloanZ loan_moneylenderZ anyloans_worryZ anyloans_dueyr0_nmZ numloans_dueyr0_nmZ numloans_dueyr0_missZ bl_loanamt_nmZ bl_loanamt_missZ) if pd>0 , cl(pid) pnotpen(i.roundgroup i.pd blatt_missZ)
		* controls chosen: blatt blatt_miss blhrprod_miss read_odiya_nm read_odiya_miss bs_labor_30_miss future_fin_worry_miss 
		reghdfe att_ind cash_postpay cash_announce blatt blatt_miss blhrprod_miss read_odiya_nm read_odiya_miss bs_labor_30_miss future_fin_worry_miss   i.pd if pd>0 , vce(cl pid) absorb(roundgroup)
	
	// won't use lasso for attendance -- sample size too small
	// expenditure lasso separately done after reshaping data
	

	
	
***************************************************************** 
*		14. SAVE MAIN ROUNDS DATA
*****************************************************************


save "$dir/data/analysis_main.dta", replace


***************************************************************** 
*		15. SAVE EXPENDITURE DATA
*****************************************************************

use "$dir/data/analysis_main.dta", clear

keep if pid1==1		// use main rounds
drop if mi(c1_food_tot_exp) 	// did not complete survey

keep pid round roundgroup wave cash postcashdaysago group totalexp_? foodexp_? tobaccoexp_? loanexp_? medicexp_? agriexp_? otherexp_? constrexp_? dressexp_? eduexp_? billexp_? dailyexp_? transpexp_? relativeexp_? festexp_? childrenexp_? otherimpexp_? loancredit_? blhrprod* blhrprodsq* blhrprod_miss* blatt* blattsq* blatt_miss* educ* educsq* exper* expersq* married* widowed* divorced* wealth_hi_avg* wealth_ind_avg* richhouse* richhouse_miss* canget1k* canget1k_miss* hasnocredits* ownland* ownland_miss* age_nm* age_miss* read_odiya_nm* read_odiya_miss* anychildren_nm* children_miss* caslabor_nm* caslabor_miss* bs_labor_7_nm* bs_labor_7_miss* bs_labor_30_nm* bs_labor_30_miss* future_fin_worry_nm* worry1_nm* future_fin_worry_miss* hasloan* loan_moneylender* anyloans_worry* anyloans_dueyr0_nm* numloans_dueyr0_nm* numloans_dueyr0_miss* bl_loanamt_nm* bl_loanamt_miss* lent borrow bl_loanamt_m durable_? spending_new_loan_? survsample

reshape long totalexp_ foodexp_ tobaccoexp_ loanexp_ medicexp_ agriexp_ otherexp_ creditpaid_ loanpaid_ loancredit_ constrexp_ dressexp_ eduexp_ billexp_ dailyexp_ transpexp_ relativeexp_ festexp_ childrenexp_ otherimpexp_ durable_ food_new_loan_ tobacco_new_loan_ loan_new_loan_ medi_exp_new_loan_ agri_input_new_loan_ oth_new_loan_ spending_new_loan_, i(pid) j(dayago)

egen pid1 = tag(pid)
egen roundgroupdayago = group(round dayago group)


//Remove missing 
foreach x in constrexp_ dressexp_ eduexp_ billexp_ dailyexp_ transpexp_ relativeexp_ festexp_ childrenexp_ otherimpexp_ {
	replace `x' = 0 if `x' == . 
}

rename *_ *


// for day level regressions

gen postpay = 0	// if we are only looking at 3 days after post period
replace postpay = 1 if inlist(dayago,1,2,3,4) & postcashdaysago == 4
replace postpay = 1 if inlist(dayago,1,2,3) & postcashdaysago == 3
replace postpay = 1 if inlist(dayago,1,2) & postcashdaysago == 2

gen postpay_3d = 0	// if we are only looking at 3 days after post period
replace postpay_3d = 1 if inlist(dayago,2,3,4) & postcashdaysago == 4
replace postpay_3d = 1 if inlist(dayago,1,2,3) & postcashdaysago == 3
replace postpay_3d = 1 if inlist(dayago,1,2) & postcashdaysago == 2

gen cash_1st = 0 
replace cash_1st = 1 if cash==1 & dayago == 4 & postcashdaysago == 4 
replace cash_1st = 1 if cash==1 & dayago == 3 & postcashdaysago == 3 
replace cash_1st = 1 if cash==1 & dayago == 2 & postcashdaysago == 2 

gen cash_2nd = 0 
replace cash_2nd = 1 if cash==1 & dayago == 3 & postcashdaysago == 4 
replace cash_2nd = 1 if cash==1 & dayago == 2 & postcashdaysago == 3 
replace cash_2nd = 1 if cash==1 & dayago == 1 & postcashdaysago == 2 

gen cash_3rd = 0 
replace cash_3rd = 1 if cash==1 & dayago == 2 & postcashdaysago == 4 
replace cash_3rd = 1 if cash==1 & dayago == 1 & postcashdaysago == 3 


*label var cash_0th "Cash $\times$ 1 day pre-pay" 
label var cash_1st "Cash $\times$ Day of payment" 
label var cash_2nd "Cash $\times$ 1 day post-pay" 
label var cash_3rd "Cash $\times$ 2 days post-pay" 


gen all_1st = 0 
replace all_1st = 1 if dayago == 4 & postcashdaysago == 4 
replace all_1st = 1 if dayago == 3 & postcashdaysago == 3 
replace all_1st = 1 if dayago == 2 & postcashdaysago == 2 


// expenditure table outcomes

gen loancredit_any = loancredit>0 if !mi(loancredit)

gen othhhexpense = eduexp + billexp + transpexp + childrenexp 
gen tothhexpense = foodexp + tobaccoexp + medicexp + dressexp + dailyexp + othhhexpense
gen allothexpense = dressexp + dailyexp + eduexp + billexp  + transpexp + childrenexp + constrexp + relativeexp + festexp + otherimpexp
gen totexpense = foodexp + tobaccoexp + medicexp + agriexp + loanexp + allothexpense

// total spending top-coded at 99%

	summ totexpense if postpay_3d==1, det
	gen totexpense_top = totexpense
	replace totexpense_top = r(p99) if totexpense>r(p99) & !mi(totexpense)
	


*** pds lasso code for expendtiure


** Error with roundgroupdayago FE. So just did roundgroup FE
pdslasso totexpense_top cash (i.roundgroup blhrprodZ blhrprodsqZ blhrprod_missZ blattZ blattsqZ blatt_missZ educZ educsqZ experZ expersqZ marriedZ widowedZ divorcedZ wealth_hi_avgZ wealth_ind_avgZ richhouseZ richhouse_missZ canget1kZ canget1k_missZ hasnocreditsZ ownlandZ ownland_missZ age_nmZ age_missZ read_odiya_nmZ read_odiya_missZ anychildren_nmZ children_missZ caslabor_nmZ caslabor_missZ bs_labor_7_nmZ bs_labor_7_missZ bs_labor_30_nmZ bs_labor_30_missZ future_fin_worry_nmZ worry1_nmZ future_fin_worry_missZ hasloanZ loan_moneylenderZ anyloans_worryZ anyloans_dueyr0_nmZ numloans_dueyr0_nmZ numloans_dueyr0_missZ bl_loanamt_nmZ bl_loanamt_missZ) if postpay_3d==1, cl(pid) pnotpen(i.roundgroup)

pdslasso totexpense_top cash_1st cash_2nd cash_3rd (i.roundgroup blhrprodZ blhrprodsqZ blhrprod_missZ blattZ blattsqZ blatt_missZ educZ educsqZ experZ expersqZ marriedZ widowedZ divorcedZ wealth_hi_avgZ wealth_ind_avgZ richhouseZ richhouse_missZ canget1kZ canget1k_missZ hasnocreditsZ ownlandZ ownland_missZ age_nmZ age_missZ read_odiya_nmZ read_odiya_missZ anychildren_nmZ children_missZ caslabor_nmZ caslabor_missZ bs_labor_7_nmZ bs_labor_7_missZ bs_labor_30_nmZ bs_labor_30_missZ future_fin_worry_nmZ worry1_nmZ future_fin_worry_missZ hasloanZ loan_moneylenderZ anyloans_worryZ anyloans_dueyr0_nmZ numloans_dueyr0_nmZ numloans_dueyr0_missZ bl_loanamt_nmZ bl_loanamt_missZ) if postpay_3d==1, cl(pid) pnotpen(i.roundgroup)
							   
* variables chosen: blhrprod_miss canget1k_miss ownland_miss bs_labor_7_miss bs_labor_30_miss future_fin_worry_miss
areg totexpense_top cash_1st cash_2nd cash_3rd blhrprod_missZ canget1k_missZ ownland_missZ bs_labor_7_missZ bs_labor_30_missZ future_fin_worry_missZ if postpay_3d==1, cl(pid) a(roundgroupdayago)


save "$dir/data/analysis_expenditure.dta", replace


